Sasha
Sasha

Reputation: 8705

Yii2 - finfo_file(/tmp/phpqE6gyD): failed to open stream: No such file or directory on save after file upload

I am getting following error, when I try to save data into db after file upload:

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

This is the code:

$userFolderPath = \Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . \Yii::$app->user->getIdentity()->iduser;

        $model = new CsFile();
        $files = UploadedFile::getInstances($model, 'files');
        $errors = [];

        if (!file_exists($userFolderPath))
            mkdir($userFolderPath, 0777, true);

        foreach($files as $file):
            $fileModel = new CsFile();
            $fileModel->files = $file;

            if($fileModel->validate()):

                $filename = str_replace(' ', '_', $file->baseName);

                if(file_exists($userFolderPath . DIRECTORY_SEPARATOR . $filename . "." . $file->extension)):
                     $filename .= "-" .uniqid();
                endif;

                $fileModel->files
                    ->saveAs($userFolderPath .DIRECTORY_SEPARATOR. $filename . '.' . $fileModel->files->extension);

                $fileModel->iduser    = Yii::$app->user->getIdentity()->iduser;
                $fileModel->name      = $filename;
                $fileModel->extension = $file->extension;
                $fileModel->add_date  = date('Y-m-d H:i:s');
                $fileModel->save();

            else:

            endif;

        endforeach;

        var_dump('<pre>', $errors, '</pre>');

Upvotes: 0

Views: 4597

Answers (5)

Angelus Roman
Angelus Roman

Reputation: 191

Using false parameter in $model->save(false) that means you are ignoring model validation, which is not right.

But using false as a second parameter in $file->saveAs($path,false) means you are trying to keep the file in the temp folder after being uploaded and allow the model to access the file during validation when trying to save to the database.

If the model fails to access the file (i.e removed from the temp folder after being uploaded), you will getting an ERROR Fail to open a stream, No such file/folder

Upvotes: 0

user2368724
user2368724

Reputation: 31

I was having same problem, I solved it with this:

$model->file->saveAs($filepath , false)

then...

$model->save(false)

Important: In the saveAs function pass false parameter.

Upvotes: 1

Yiafee Khan
Yiafee Khan

Reputation: 31

Clyff is right. But in case you are saving the path of the file in database to read later, setting the attribute to null is not going to work.

The problem is when you try to save the model still with result of UploadedFile::getInstance($model, 'file') in the file field which is already used by $model->file->saveAs(); $model->save() cannot save the path of the saved and already removed temporary files path directly. So after successful $model->file->saveAs($path) you need to do something like:

$model->file = $path;

It was quite unclear to me and spent a bit of time after fileinfo , so hope the answer helps.

Upvotes: 0

Yii2 use UploadFile class through function $model->upload() to save upload file

To fix this use inside your $model->upload function :

return copy($this->YourAttribute->tempName, $newFileName);

instead

return $model->attribute->saveAs($newFileName)

Upvotes: 0

Clyff
Clyff

Reputation: 4076

I had the same problem a few weeks ago. Turns out, when we rename the file before upload and try to save the model, this error will appear.

If that attribute it's only for handle your upload and have no field in your table, you can just unset this fields before saving: $files Model->files = null.

Let me know if your scenario is different than mine.

Upvotes: 3

Related Questions