Yii2: finfo_file(C:\xampp\tmp\php29C.tmp): failed to open stream: No such file or directory

99封情书 提交于 2019-12-25 18:47:16

问题


Getting error finfo_file(C:\xampp\tmp\php29C.tmp): failed to open stream: No such file or directory while uploading multiple files.

tried inserting $model->save(); before saveAs() but then it upload only one file not all files, also not getting path in database for each file which is getting uploaded.

Controller:

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

            if ($model->load(Yii::$app->request->post()))
            {
                $imageName = $model->room_type;
                     $model->file = UploadedFile::getInstances($model, 'file');

                foreach ($model->file as $file_instance) 
                {
                        $model->save();
                        $file_instance->saveAs('uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension);
                        //save the path in the db column
                        $file_instance->$model->images = 'uploads/room_img/'.$imageName.'.'.$file_instance->extension;


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

回答1:


It is possible that I already have answered this here within the last update after your comments but I'll add a different answer here as it is one more different issue beside the others. This error is thrown when using $file_instance->baseName or $file_instance->extension after saving the file. saveAs() whish its content is the following :

public function saveAs($file, $deleteTempFile = true)
{
    if ($this->error == UPLOAD_ERR_OK) {
        if ($deleteTempFile) {
            return move_uploaded_file($this->tempName, $file);
        } elseif (is_uploaded_file($this->tempName)) {
            return copy($this->tempName, $file);
        }
    }
    return false;
}

has a boolean $deleteTempFile argument set to true by default. which means it will delete the temporary file after saving and you will not be able to save the uploaded file again in the same request. You can set it to false if needed by doing :

$file_instance->saveAs('...',false)

BUT it won't be a good practice in this case as you are getting 2 copies of the same file. If the file is already saved to its new path then why not deleting it from the tmp folder. Just hold that path in a variable before calling saveAs() and use it when needed like it is done in the answer linked above :

$path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension;
$file_instance->saveAs($path);
$model->images = $path;

Other issues you need to solve are :

foreach ($model->file as $file_instance) 
{
     $model->save();
     $model->images = $path;
}

The $model->save() here have no logic to me. You have a single instance of an object and foreach file instance stored in its attribute $model->file you are saving the hole model again and again. And I understand from the second one ($model->images = $path) that your model have an attribute images to which you can assigning a string value. but you have multiple images here where each of them has its own path and inside the loop you are overriding it each time with the next image path string. If is the case I would probably store the path to folder where I can find all those images later instead :

$model->images = 'uploads/room_img/[a_collection_id_or_wathever_unique]/'

And inside that folder I would host the related images. Or maybe I'll host all paths separated by ; or equivalent by doing this inside the loop :

$model->images .= $path . ';';

In case if images are related models represented by an ActiveRecord class then the answer linked above should fix it.



来源:https://stackoverflow.com/questions/35322316/yii2-finfo-filec-xampp-tmp-php29c-tmp-failed-to-open-stream-no-such-file-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!