Error trying to save an image in a directory

Hello,

I’m trying to upload an image file in a form.

MODEL




<?php


namespace app\models;


use yii\db\ActiveRecord;

use yii\behaviors\TimestampBehavior;

use yii\web\UploadedFile;


class Model extends ActiveRecord 

{   

    

    /*

     * @return string the name of the file

     */

    public $file;




    public function rules()

    {

        return[

            //.....

            [['file'],'file','skipOnEmpty' => true],

            //......

        ];

    }




Controller:

(i’ve included the yii\web\UploadedFile;)




public function actionCreate()

    {   

        $model   = new Model();


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

        { 

          // Upload

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

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

          

          //Saving the path......

          $model->image = 'uploads/model/'.$model->file->baseName.'.'.$model->file->extension;

         

         //Other Part of the action........


         return $this->render('create',[

                  'model' => $model,

                      ]);




The folders are located inside the view directory

(path /views/uploads/model/)

and they have the 777 permission (working with ubuntu)

and for final the field on the view.





<?= $form->field($model, 'file')->fileInput()->label('Upload an Image') ?>




when i’m trying to save a model record with an image file i get this error:

move_uploaded_file(uploads/images.jpeg): failed to open stream: No such file or directory

where i must collocate the directory for upload a file?

Thanks for all the help

You have to save the model before you can use $model->file->saveAs


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

$model->save();

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

"upload/model/" is a relative path from the entry script "index.php". So it refers to the directory "@webroot/upload/model/", usually something like "/var/www/html/upload/model/".

Try:




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



But I think you’d be better to create a directory for uploaded images under “/var/www/html/”.

I would like to separate the application data and the user data.

You have to save the model before you save the image($model->file->saveAs) and check with the folder uploads has been created and it has read & write permission.

?

A model can’t be saved unless it is an ActiveRecord instance.

What’s your point, this is an ActiveRecord model…

A model without a backing db table can handle file uploading. Can’t it? Think of a model just for a html form.

Uploading Files

No need to save the model before saving the uploaded file.

Yes it can.

I got the same error a time ago with upload image and saving path info of that file to db table. Therefore I needed to save model before using saveAs.

This is another scenario… sorry if I’m confusing others.