Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File uploading issue in Yii2

I am developing an application in Yii2 framework.

I am uploading an image using code below and which has worked perfect till now. But now I got an error instant. I don't understand what has happened.

Below is the code of controller where upload file and save take place:

// Upload photo of subcategories...
$model->file = UploadedFile::getInstance($model, 'file');
if($model->file) {
    $imageName = rand(1000,100000);
    $model->file->saveAs('uploads/subcategories/'.$imageName.'.'.$model->file->extension);
    $model->sub_category_photo = 'uploads/subcategories/'.$imageName.'.'.$model->file->extension;
}
$model->save();

I got below error:

PHP Warning – yii\base\ErrorException finfo_file(C:\xampp\tmp\php9A7B.tmp): failed to open stream: No such file or directory

I also un-comment extension = fileinfo.dll in php.ini file and restart server.

like image 697
user1780370 Avatar asked Feb 10 '23 04:02

user1780370


1 Answers

I got solutions of this questions.

Call $model->save(); in controller

before

$model->file->saveAs();

Means in above code of my questions changed to below

// Upload photo of subcategories...
$model->file = UploadedFile::getInstance($model, 'file');
if($model->file) {
    $imageName = rand(1000,100000);
    $model->sub_category_photo = 'uploads/subcategories/'.$imageName.'.'.$model->file->extension;
    $model->save();
    $model->file->saveAs('uploads/subcategories/'.$imageName.'.'.$model->file->extension);
} else {
    $model->save();
}

Hope this help someone as it helped me and I found answer from here

like image 93
user1780370 Avatar answered Feb 27 '23 20:02

user1780370