Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

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

Getting error finfo_file(C:xampp mpphp29C.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,
                ]);
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...