file upload failed

Hi Guys,

hope you all always in good health.

  • short story

this is basic knowledge that i don’t know. i have created an upload function but it doesn’t work. i was follow this tutorial http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html.

  • problem

here are what i did.

Model:

Controller:

Form:

ok, all saved. i tried to upload, choose the file and click create. after create button clicked, the page wasn’t redirect to any page and error message always same. it seems i don’t choose any files before click the create button but i did.

help me please, which part that i missed or wrong.

thank you.

note:

the brown are done by me.

Hi yogaz,

Change your controller action like this, it may solve your problem


    public function actionCreate()

    {

        $model = new Img();


        if ($model->load(Yii::$app->request->post())) {

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

		if($model->img_path){

		    // $model->upload(); OR 

		    $model->img_path->saveAs('uploads/'.time().'.'.$model->attachment->extension);

		    $model->img_path = 'uploads/'.time().'.'.$model->attachment->extension;

		}

        	if ($model->save()) {

        	    return $this->redirect(['view', 'id' => $model->id]);

        	}

        } else {

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

        }

    }

Hi Selvakumar,

yes, it did it :D.

thank you so much.

the image successfully uploaded, but the path isn’t save in database yet. im trying to learn the code and try.

i will comeback if i get stuck. haha

once again thanks :)