Error in fileValitadors

Why the mimeTypes, extensions validators don’t work, when I try use like in doc it work only halp part.

Ex. In view if I try use an file that not a png for example they show the error like expected, when a use a png the view works fine, but when I try submit it they show an error.

[i]PHP Warning – yii\base\ErrorException

finfo_file(/tmp/php2Gnw9T): failed to open stream: No such file or directory[/i]


if ($checkExtension) {

                return static::getMimeTypeByExtension($file, $magicFile);

            } else {

                throw new InvalidConfigException('The fileinfo PHP extension is not installed.');

            }

        }

        $info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);

 

        if ($info) {

            $result = finfo_file($info, $file);

            finfo_close($info);

 

            if ($result !== false) {

                return $result;

            }

        }

 

        return $checkExtension ? static::getMimeTypeByExtension($file, $magicFile) : null;

    }

And


public function uploadBanner()

    {

        if ($this->validate()) {

            if ($this->coverFile != null) {

                $file = explode('.', $this->coverFile->name);

                $this->coverFile->saveAs(\Yii::getAlias('@webroot/img/portfolio/') . $file[0] . '-' . date('YmdHis') . '.' . $this->coverFile->extension);

 

                return $file[0] . '-' . date('YmdHis') . '.' . $this->coverFile->extension;

            }

and


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

            //Pega a instância do arquivo

            $model->coverFile = UploadedFile::getInstance($model, 'coverFile');

 

            //Salva o caminho no BD e apaga a imagem antiga

            if ($model->uploadBanner()) {

                $model->cover = $model->uploadBanner();

                $model->deleteBanner($currentImage);

            }

What I’m doing wrong?

My rules


public function rules()

    {

        return [

            [['title', 'description'], 'required'],

            [['coverFile'], 'required', 'on' => 'insert'],

            [['coverFile'], 'file', 'mimeTypes' => ['image/jpeg', 'image/png']],

            [['description'], 'string'],

            [['title', 'subtitle'], 'string', 'max' => 100],

            [['cover'], 'string', 'max' => 150],

        ];

    }

Looks like a problem with the fileinfo PHP extension.

Is your "/tmp" directory writable to the server process?

I think so. I put 777 on may root app to test.

You are calling "uploadBanner()" twice.

The validation of the 1st call succeeds as expected, but the 2nd one fails since UploadedFile::saveAs() has already cleaned up the uploaded temporary file (’/tmp/php2Gnw9T’).

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

Ok. I’ll see it amd refactor the code.

thanks again @softark

@softark The problems not appear on the place you tell me.

It occur only I put the validation ‘mimeTypes’ => [‘image/jpeg’, ‘image/png’], whitout it I can save everything.

Without mimeType checking it may run without explicit error, but it is just by a lucky accident. There should be a hidden error when the same code doesn’t work with ‘mimeTypes’ => [‘image/jpeg’, ‘image/png’]. It must work with mimeType checking.

The error message of “finfo_file(/tmp/php2Gnw9T): failed to open stream: No such file or directory” tells you that the uploaded temporary file is missing when “finfo_file()” tries to check the mimeType by inspecting the file content. It’s because you are calling “uploadBanner” twice, in which you call UploadedFile::saveAs() that will move the uploaded temporary file to the final destination with the name that you want.

Please trace the code execution with XDebugger.