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],
];
}