服务器端文件验证在Yii.2.0中不起作用 [英] Server Side file validation Don't work in Yii.2.0
问题描述
FileValidator
类只支持客户端验证? 我用文件上传系统使用代码:
在模型中: 当一个无效文件(例如由 一旦表单设置如下: 没有给出错误信息,文件将不会被服务器端验证。 我也尝试设置变量( 我通过 尝试添加 至少我有这个在我的窗体运行,我没有禁用客户端验证。 http://www.yiiframework.com/doc-2.0/yii-validators-filevalidator.html Is the I written the file upload system with the use of code : in model: in controller: in view : When a invalid file(for example file by Once the form is set as follows: No error message is given and the files will not be Server side validated. also i tried to set the variable ( I uploaded invalid file for example file by
Try to add At least i have this running in my form where i haven't disabled client side validation.
Not sure if you have enabled all required php extensions http://www.yiiframework.com/doc-2.0/yii-validators-filevalidator.html 这篇关于服务器端文件验证在Yii.2.0中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b $ pre $ public function rules( ){
return [
$ b $ [['allAllowedFileType'],'safe'],
[[allAllowedFileType]],'file' ,
'extensions'=>'jpg',
'mimeTypes'=> 'image / jpeg'],
];
$ b $在控制器中:
$ b pre> public function actionCreate(){
$ model = new File();
if($ model-> load(Yii :: $ app-> request-> post())){
//获取上传的文件实例。对于多个文件上传
//下面的数据将返回一个数组
$ image = UploadedFile :: getInstance($ model,'allAllowedFileType');
//存储源文件名
$ model-> name = $ image-> name;
$ ext = end((explode(。,$ image-> name)));
$ b $ //生成一个唯一的文件名
$ avatar = Yii :: $ app-> security-> generateRandomString()。。{$ ext};
$ path ='c:wamp / www /'。 $化身;
if($ model-> validate()&& $ model-> save(0)){
$ image-> saveAs($ path);
return $ this-> redirect(['view','id'=> $ model-> id]);
} else {
//保存模型中的错误
}
}
return $ this-> render('create',[
'model '=> $ model,
]);
$ b $ p
$ b
< pre $ <?php
$ form = ActiveForm :: begin(['enableClientValidation'=> true,
'options'=> ['enctype' =>'multipart / form-data'] //重要
]);
$ b $ echo $ form-> field($ model,allAllowedFileType) - > fileInput();
?>
.php
extention)被加载。
只有客户端验证完成。
$ form = ActiveForm :: begin(['enableClientValidation'=> false,
'options'=> ['enctype'=>'multipart / form-data' ] //重要
]);
$ model-> allAllowedFileType
),如下所示:
$ model-> allAllowedFileType = $ _ FILES ['File'];
.exe $ c $上传了无效文件c> extention)并执行validate函数,错误消息将不会被显示,并且
$ model->错误
为空。
public function actionCreate(){
$ model = new File();
if($ model-> load(Yii :: $ app-> request-> post())){
$ image = UploadedFile :: getInstance ($ model,'allAllowedFileType');
...
$ model-> allAllowedFileType = $ _ FILES ['File']; ($ model-> validate()){
//保存文件
} else {
die(var_dump($ model-> errors));
$ b $ return $ this-> render('create',[
'model'=> $ model,
]);
'skipOnEmpty'=>假的
也许 checkExtensionByMimeType
到您的规则。
[[allAllowedFileType]],'file','skipOnEmpty'=>假,扩展=> 'jpg','checkExtensionByMimeType'=>真正的],
不知道是否启用了所有必需的php扩展
$ b FileValidator
class in Yii2 only supports clientside validation ? public function rules() {
return [
[['allAllowedFileType'], 'safe'],
[['allAllowedFileType'], 'file',
'extensions'=>'jpg',
'mimeTypes' => 'image/jpeg'],
];
}
public function actionCreate() {
$model = new File();
if ($model->load(Yii::$app->request->post())) {
// get the uploaded file instance. for multiple file uploads
// the following data will return an array
$image = UploadedFile::getInstance($model, 'allAllowedFileType');
// store the source file name
$model->name = $image->name;
$ext = end((explode(".", $image->name)));
// generate a unique file name
$avatar = Yii::$app->security->generateRandomString().".{$ext}";
$path = 'c:wamp/www/' . $avatar;
if($model->validate()&&$model->save(0)){
$image->saveAs($path);
return $this->redirect(['view', 'id'=>$model->id]);
} else {
// error in saving model
}
}
return $this->render('create', [
'model'=>$model,
]);
}
<?php
$form = ActiveForm::begin(['enableClientValidation' => true,
'options' => ['enctype' => 'multipart/form-data'] // important
]);
echo $form->field($model, "allAllowedFileType")->fileInput();
?>
.php
extention) is loaded .
Only client-side validation is done. $form = ActiveForm::begin(['enableClientValidation' => false,
'options' => ['enctype' => 'multipart/form-data'] // important
]);
$model->allAllowedFileType
) as follows :$model->allAllowedFileType=$_FILES['File'];
.exe
extention) and execute validate function, the error message wont be displayed.and $model->errors
is empty.public function actionCreate() {
$model = new File();
if ($model->load(Yii::$app->request->post())) {
$image = UploadedFile::getInstance($model, 'allAllowedFileType');
...
$model->allAllowedFileType=$_FILES['File'];
if($model->validate()){
//save file
} else {
die(var_dump($model->errors));
}
}
return $this->render('create', [
'model'=>$model,
]);
}
'skipOnEmpty' => false
and maybe checkExtensionByMimeType
to your rules.[['allAllowedFileType'], 'file', 'skipOnEmpty' => false, 'extensions' => 'jpg', 'checkExtensionByMimeType' => true],
fileinfo
is required.