Yii Framework 2.0上传文件错误finfo_file():未能打开流:没有这样的文件或目录 [英] Yii Framework 2.0 Uploading Files Error finfo_file(): failed to open stream: No such file or directory
问题描述
$ model-> file-> saveAs('uploads /'。$ model-> file-> baseName。'。'。$ model-> file-> extension);
$ model-> save();
我得到以下错误:
PHP警告 - yii\base\ErrorException
$ b $ finfo_file(/ tmp / phpIGuwiT):未能打开流:没有这样的文件或目录
1. in /var/www/html/website/advanced/vendor/yiisoft/yii2/helpers/BaseFileHelper.php
if($ info){
$ result = finfo_file($ info, $文件);
finfo_close($ info);
if($ result!== false){
return $ result;
}
}
2. in /var/www/html/my-project/advanced/vendor/yiisoft/yii2/validators/FileValidator.php - yii\help \BaseFileHelper :: getMimeType()
$ mimeType = FileHelper :: getMimeType($ file-> tempName,null,false);
3.
4.
等等...
结果是图像已经上传,但模型没有被插入到数据库中。有没有人知道如何解决这个问题?
我有完全相同的问题,我通过调用
$ model-> save();
$ model-> file-> saveAs()
我不知道你的模型是怎么样的,但是如果你只是将实际的文件保存在服务器上,并且只保存数据库中映像的路径,那么它应该可以正常工作。
I想一下当在model-> save之前调用file-> saveAs()时,会发生什么情况,模型验证会失败,因为file不再包含已上传的数据(已保存为...)。希望这是有道理的。
Following Yii Framework 2.0 documentation http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html I tried to upload image. The image could be uploaded successfully, but after uploading the image, I tried to insert the model into the database with the following code.
$model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
$model->save();
I got the following error:
PHP Warning – yii\base\ErrorException
finfo_file(/tmp/phpIGuwiT): failed to open stream: No such file or directory
1. in /var/www/html/website/advanced/vendor/yiisoft/yii2/helpers/BaseFileHelper.php
if ($info) {
$result = finfo_file($info, $file);
finfo_close($info);
if ($result !== false) {
return $result;
}
}
2. in /var/www/html/my-project/advanced/vendor/yiisoft/yii2/validators/FileValidator.php – yii\helpers\BaseFileHelper::getMimeType()
$mimeType = FileHelper::getMimeType($file->tempName, null, false);
3.
4.
and so on ....
The result is that image has been uploaded but the model has not been inserted into the database. Does anyone know how to solve this?
I was having the exact same problem, and I solved it by calling
$model->save();
before
$model->file->saveAs()
I don't know how your model looks like, but if you are just saving the actual file on the server, and only the path to the image in the DB, it should work.
I think what happens is that when you call file->saveAs() before model->save, the model validation fails because 'file' no longer contains the uploaded data (it is already saved as...). Hope it makes sense.
这篇关于Yii Framework 2.0上传文件错误finfo_file():未能打开流:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!