Yii Framework 2.0 Uploading Files Error finfo_file

2020-02-10 14:34发布

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?

10条回答
女痞
2楼-- · 2020-02-10 14:46

try $model->file->saveAs($filepath, false) instead of $model->file->saveAs($filepath)
or set $model->file = null before calling $model->file->saveAs() will solve the problem

查看更多
我命由我不由天
3楼-- · 2020-02-10 14:56

You need to call $model->save() first then $model->file->saveAs(). Below is the code for reference.

if($model->file) {   
    $imagename = 'Profile_'.$model->created_at.rand(100000, 999999);
    $model->profilepic = $imagename.'.'.$model->file->extension;
    $path = 'uploads/';
}

if($model->save()) {
    $model->file ? $model->file->saveAs($path.$imagename.'.'.$model->file->extension): null;
    //the render or redirect code goes here on success
} else {
    //if the validation fails then it will be executed
}

查看更多
等我变得足够好
4楼-- · 2020-02-10 14:57

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.

查看更多
Summer. ? 凉城
5楼-- · 2020-02-10 15:02

My solution looks like this:

    $model = new Film();
    $uploads = Yii::getAlias('@uploads'); // Alias root_dir/uploads

    if ($model->load(Yii::$app->request->post())) {
        $file = UploadedFile::getInstance($model, 'poster');

        $imageName = rand(9999, 999999);

        $file->name = $imageName . '.' . $file->extension; // override the file name

        $model->poster = $file;

        if($model->validate() && $model->save()) {
            $file->saveAs($uploads . '/posters/' . $model->poster);
            return $this->redirect(['view', 'id' => $model->film_id]);
        }

    }

    return $this->render('create', [
        'model' => $model
    ]);
查看更多
登录 后发表回答