yii2文件上传验证问题解决方法
how to solve File upload validation problem in yii2
我正在尝试在 yii2 中上传一个文件,相同的代码之前工作正常但出于某种原因我无法弄清楚它在大量检查后停止工作我注意到上传模型的验证方法是返回 false 并在错误消息中显示 Array ( [file] => Array ( [0] => Only files with these extensions are allowed: png, jpg,jpeg,gif,pdf. ) )
但最奇怪的是我上传了一个 jpg 文件 我也尝试上传一个 png 文件同样的错误,当我删除模型规则中的扩展检查时它工作fineor 完全删除验证也可以,我不知道这里缺少什么任何帮助将不胜感激。
NOTE //with the validation or the extension check for extension in place the codes in the if()
statement fails to execute but rather the else statement execute, removing the validation or the
extension check the code in if() works fine
class Upload extends Model
{
public $file;
public $randomCharacter;
public $fileDirectory;
public function rules()
{
return[
[['file'], 'file', 'skipOnEmpty' => false,'maxSize'=>1024 * 1024 * 2, 'extensions'=> ['png, jpg,jpeg,gif,pdf']],
];
}
public function upload($uploadPath=NULL)
{
if(isset($uploadPath) && $uploadPath !==NULL){
$filePath = $uploadPath;
}else{
$filePath = "@frontend/web/uploads/";
}
//generate random filename
$rand = Yii::$app->security->generateRandomString(10). '_' . time();
//assign generated file name to randomCharacter property
$this->randomCharacter = $rand;
//define the upload path;
if ($this->validate()){
$path = \Yii::getAlias($filePath);
$this->fileDirectory = $this->randomCharacter .'.'.$this->file->extension;
echo $path.$this->fileDirectory;
exit;
$this->file->saveAs($path.$this->fileDirectory );
return true;
}else{
// return false;
//with validation in place the else statement in executed
print_r($this->getErrors());
exit;
}
}
}
删除属性“extensions”的直括号
而不是:
[['file'], 'file', 'skipOnEmpty' => false,'maxSize'=>1024 * 1024 * 2, 'extensions'=> ['png, jpg,jpeg,gif,pdf']],
你应该有这个:
[['file'], 'file', 'skipOnEmpty' => false,'maxSize'=>1024 * 1024 * 2, 'extensions'=> 'png, jpg, jpeg, gif, pdf'],
我正在尝试在 yii2 中上传一个文件,相同的代码之前工作正常但出于某种原因我无法弄清楚它在大量检查后停止工作我注意到上传模型的验证方法是返回 false 并在错误消息中显示 Array ( [file] => Array ( [0] => Only files with these extensions are allowed: png, jpg,jpeg,gif,pdf. ) )
但最奇怪的是我上传了一个 jpg 文件 我也尝试上传一个 png 文件同样的错误,当我删除模型规则中的扩展检查时它工作fineor 完全删除验证也可以,我不知道这里缺少什么任何帮助将不胜感激。
NOTE //with the validation or the extension check for extension in place the codes in the if()
statement fails to execute but rather the else statement execute, removing the validation or the
extension check the code in if() works fine
class Upload extends Model
{
public $file;
public $randomCharacter;
public $fileDirectory;
public function rules()
{
return[
[['file'], 'file', 'skipOnEmpty' => false,'maxSize'=>1024 * 1024 * 2, 'extensions'=> ['png, jpg,jpeg,gif,pdf']],
];
}
public function upload($uploadPath=NULL)
{
if(isset($uploadPath) && $uploadPath !==NULL){
$filePath = $uploadPath;
}else{
$filePath = "@frontend/web/uploads/";
}
//generate random filename
$rand = Yii::$app->security->generateRandomString(10). '_' . time();
//assign generated file name to randomCharacter property
$this->randomCharacter = $rand;
//define the upload path;
if ($this->validate()){
$path = \Yii::getAlias($filePath);
$this->fileDirectory = $this->randomCharacter .'.'.$this->file->extension;
echo $path.$this->fileDirectory;
exit;
$this->file->saveAs($path.$this->fileDirectory );
return true;
}else{
// return false;
//with validation in place the else statement in executed
print_r($this->getErrors());
exit;
}
}
}
删除属性“extensions”的直括号
而不是:
[['file'], 'file', 'skipOnEmpty' => false,'maxSize'=>1024 * 1024 * 2, 'extensions'=> ['png, jpg,jpeg,gif,pdf']],
你应该有这个:
[['file'], 'file', 'skipOnEmpty' => false,'maxSize'=>1024 * 1024 * 2, 'extensions'=> 'png, jpg, jpeg, gif, pdf'],