finfo_file(/tmp/phpuYkBNk): failed to open stream: No such file or directory

Hi there,

I’m trying to upload image in yii2, I’m using Ubuntu 15.10.

Image uploaded to uploads directory but I got this error and data not saved into database.


public function actionCreate()

    {

        $model = new Post();

        $catModel = new Category();


        if ($model->load(Yii::$app->request->post())) {


            $path = Yii::getAlias('@root') .'/uploads/';

            $model->image = \yii\web\UploadedFile::getInstance($model, 'image');

            

            $model->image->saveAs($path . $model->image->baseName . '.' . $model->image->extension);


            if($model->save())

                return $this->redirect(['view', 'id' => $model->id]);

            return $this->render('create', [

                'model' => $model,

                'catModel' => $catModel

            ]);

        } else {

            return $this->render('create', [

                'model' => $model,

                'catModel' => $catModel

            ]);

        }

    }

Error says I have not installed:The fileinfo PHP extension is not installed.

but as my other search fileinfo extension already installed in newer ubuntu versions.

pls see attaced screenshot for full detail, how I can fix this issue?

6944

yii-upload-image-error.png

you can find this question on:Stackoverflow

You have a “file” validator in your Post model to check the MIME type of the uploaded file, don’t you?

It tries to inspect the file content using fileinfo by opening the uploaded temporary file whose path is usually something like "/tmp/phpuYkBNk".

But you are calling UploadedFile::saveAs() before the validation. It moves the uploaded temporary file to the path which you specify with the parameter of the method.

http://www.yiiframework.com/doc-2.0/yii-web-uploadedfile.html#saveAs()-detail

So there’s no more “/tmp/phpuYkBNk” when the file validator want to see it.

1 Like

Thank you very much for your time, so this is the reason, so when I should cal saveAs() method?

after $model->save()?

Yes.

Or, after validate().




if ($model->validate()) {

    $model->file->SaveAs();

    $model->save(false);  // without validation

}



1 Like