Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - Yii Framework 2.0 Uploading Files Error finfo_file(): failed to open stream: No such file or directory

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 – yiiaseErrorException

 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 – yiihelpersBaseFileHelper::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?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...