使用验证器设置图像最大文件大小

时间:2015-02-04 01:57:26

标签: image upload yii2

我正在使用文件输入窗口小部件在Yii2中实现图像上传,如http://demos.krajee.com/widget-details/fileinput所示。我可以知道如何设置上传的文件大小限制吗?

我已添加:

['image', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024 * 1024 * 1024],

在模型的rules()内,但它似乎不起作用。

希望有人可以提供建议。谢谢。

在视图中:

<?php $form = ActiveForm::begin(['enableClientValidation' => false,  'options' => [ 'enctype' => 'multipart/form-data']]); ?>


<?php

echo $form->field($model, 'image')->widget(FileInput::classname(), [
    'options'=>['accept'=>'image/*', 'multiple'=>true],
    'pluginOptions'=>['allowedFileExtensions'=>['jpg', 'jpeg', 'gif','png']]
]);

?>

<?php ActiveForm::end(); ?>

在控制器中:

    $model = new IMAGEMODEL();  

    Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/uploads/PROJECT/';


    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, 'image');


        // store the source file name
        $model->FILENAME = $image->name;
        $ext = end((explode(".", $image->name)));

        // generate a unique file name

        $model->AVATAR = Yii::$app->security->generateRandomString().".{$ext}";
        $model->STD_ID=$_POST['IMAGEMODEL']['STD_ID'];


        // the path to save file, you can set an uploadPath
        // in Yii::$app->params (as used in example below)
        $path = Yii::$app->params['uploadPath'] . $model->AVATAR;

        if($model->save()){
            $image->saveAs($path);
            Yii::$app->session->setFlash('success', 'Image uploaded successfully');
            return $this->redirect(['view', 'id'=>$id]);

        } else {
            Yii::$app->session->setFlash('error', 'Fail to save image');
        }
    }

在模特:

public function rules()
{
    return [
        [['STD_ID', 'FILENAME'], 'required'],
        [['FILENAME'], 'string'],
        [['LAST_UPD_ON'], 'safe'],
        [['STD_ID'], 'string', 'max' => 50],
        [['LAST_UPDATE_BY'], 'string', 'max' => 150],

        [['image', 'FILENAME'], 'safe'],
        ['image', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024 * 1024 * 1],

    ];
}

3 个答案:

答案 0 :(得分:7)

1) maxSize参数需要字节数。在您的示例中,您设置1 Gb。 2 Mb应该是:

['image', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024 * 1024 * 2],

2)同时检查upload_max_filesize INI设置。

3)确保在验证前通过调用yii\web\UploadedFile方法(对于多个文件使用getInstance())传递getInstances()的实例:

$this->image = UploadedFile::getInstance($this, 'image');

答案 1 :(得分:2)

您还可以使用此设置图像尺寸以及最大文件大小

['image', 'image', 'minWidth' => 250, 'maxWidth' => 250,'minHeight' => 250, 'maxHeight' => 250, 'extensions' => 'jpg, gif, png', 'maxSize' => 1024 * 1024 * 2],

这允许您上传最大2mb,宽250px,高250px

答案 2 :(得分:0)

您可以在FileInput小部件的视图pluginOptions中设置此属性:'maxFileSize' => 2048。它将是这样的:

widget(FileInput::classname(), [
    'options' => ['multiple'=>true, 'accept'=>'image/*'],

    'pluginOptions'=>[
        'allowedFileExtensions'=>['jpg','gif','png'],
        ...
        'maxFileSize' => 2048,
        ...
    ]
]);