Uploading Files

Hello,

In this guide Uploading Files is there some point not clear for me.

I created the model "models/UploadForm.php", then the view "views/upload/index.php" and the controller "controllers/UploadFormController.php". The files content is the same of the guide, but going to "www.mysite.com/upload" the "(#404) Unable to resolve the request "upload"." error occurs.

Can you help me, please?

your controller name is UploadFormController so the URL should be /upload-form

This is not related to the file upload thing.

That’s right, this is a lack in my understanding the routing process. Thank you.

I’m really, really sorry but still don’t understand. Please be patient.

This is my /models/Upload.php


<?php

namespace app\models;


use yii\base\Model;

use yii\web\UploadedFile;


/**

 * UploadForm is the model behind the upload form.

 */

class Upload extends Model

{

    /**

     * @var UploadedFile|Null file attribute

     */

    public $file;


    /**

     * @return array the validation rules.

     */

    public function rules()

    {

        return [

            [['file'], 'file'],

        ];

    }

}

?>

This the views/upload/index.php:


<?php

use yii\widgets\ActiveForm;


$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>


<?= $form->field($model, 'file')->fileInput() ?>


<button>Submit</button>


<?php ActiveForm::end(); ?>

And the controllers/UploadController.php


<?php

namespace app\controllers;


use Yii;

use yii\web\Controller;

use app\models\Upload;

use yii\web\UploadedFile;


class SiteController extends Controller

{

    public function actionUpload()

    {

        $model = new Upload();


        if (Yii::$app->request->isPost) {

            $model->file = UploadedFile::getInstance($model, 'file');


            if ($model->validate()) {                

                $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);

            }

        }


        return $this->render('index', ['model' => $model]);

    }

}

?>

On running “www.mysite.com/upload/” this code return “Unknown class - Unable to find ‘app\controllers\UploadController’ in file: /var/www/mds/controllers/UploadController.php. Namespace missing?”

Changing "class SiteController extends Controller" to "class UploadController extends Controller" return "Unable to resolve the request: upload/"

Where I’m wrong, please?

Edit:

Ok, I got it. The call must be "www.mysite.com/upload/upload"

Thank you