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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With