0

While uploading multiple file getting this error:

When I put [['file'], 'file', 'maxFiles' => 4],in model getting following error:

Call to a member function saveAs() on null

But when I put this [['file'], 'file'], in model, its uploading.

Why am I getting error?

View:

<?php  echo $form->field($model,'file[]')->label(false)->widget(FileInput::classname(), 
                          [
                          'options'=>['accept'=>'image/*', 'multiple'=>true],
                          'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
                          ]]);  
                      ?> 

Controller:

public function actionCreate()
    {
        $model = new RoomTypes();

        if ($model->load(Yii::$app->request->post()))
        {
            $imageName = $model->room_type;
            $model->file = UploadedFile::getInstance($model, 'file');
            $model->file->saveAs( 'uploads/room_img/'.$imageName.'.'.$model->file->extension);
            //save the path in the db column
            $model->images = 'uploads/room_img/'.$imageName.'.'.$model->file->extension;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } 
        else 
        {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
3
  • 1
    $model is null, are you creating the object properly on the controller? Commented Feb 10, 2016 at 14:58
  • 1
    i have updated the question. Commented Feb 10, 2016 at 15:03
  • print_r($model ) check it to debug Commented Feb 11, 2016 at 6:16

1 Answer 1

1

Use getInstances instead of getInstance as according to their respective documentations, the first returns all uploaded files for a given model attribute while the second is designed to return a single one. Then loop and save them one by one :

if ($model->load(Yii::$app->request->post())) { 
     $imageName = $model->room_type;

     $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');

     $all_files_paths = [];

     foreach ($model->imageFiles as $file_instance) {
        // this should hold the new path to which your file will be saved
        $path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension;

        // saveAs() method will simply copy the file 
        // from its temporary folder (C:\xampp\tmp\php29C.tmp) 
        // to the new one ($path) then will delete the Temp File
        $file_instance->saveAs($path);

        // here the file should already exist where specified within $path and 
        // deleted from C:\xampp\tmp\ just save $path content somewhere or in case you need $model to be
        // saved first to have a valid Primary Key to maybe use it to assign
        // related models then just hold the $path content in an array or equivalent :
        $all_files_pathes []= $path;
     }

     $model->save();

     /*
        after $model is saved there should be a valid $model->id or $model->primaryKey
        you can do here more stuffs like :

        foreach($all_files_pathes as $path) {
            $image = new Image();
            $image->room_id = $model->id;
            $image->path = $path;
            $image->save();
        }
     */

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

See docs for more info.

Sign up to request clarification or add additional context in comments.

9 Comments

Getting unknown property: backend\controllers\RoomTypesController::imageFiles getting this error
I just updated. should be $model->file instead of $this->file as you are doing it inside controller and not inside model like in official docs. and file instead of 'imageFiles'. my bad !
while saving finfo_file(C:\xampp\tmp\php90FA.tmp): failed to open stream: No such file or directory getting this error
if ($model->load(Yii::$app->request->post())) { $imageName = $model->room_type; $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); foreach ($model->imageFiles as $file_instance) { $file_instance->saveAs('uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension); $model->save(); } return $this->redirect(['view', 'id' => $model->id]); } here is the create action
hi again. why are u doing $model->save() inside the foreach loop ? $model is a single instance. $model->imageFiles is containing files instances through which we loop and we do saveAs($path) to each of them. and about the previous error it is normal as tmp folder will be cleaned after saving your file. I'll do an update
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.