Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set image maximum file size with validator

Tags:

image

upload

yii2

I am implementing image upload in Yii2 using File Input Widget as shown in http://demos.krajee.com/widget-details/fileinput. May I know how to set the uploaded file size limit?

I have added:

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

inside the model's rules() but it does not seem to work.

Hope someone can advise. Thanks.

In View:

<?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(); ?>

In Controller:

    $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');
        }
    }

In Model:

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

    ];
}
like image 442
esiaz Avatar asked Feb 04 '15 01:02

esiaz


2 Answers

1) maxSize parameter expects number of bytes. In your example you set 1 Gb. For 2 Mb it should be:

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

2) Also check upload_max_filesize INI setting.

3) Make sure pass the instance of yii\web\UploadedFile by calling getInstance() method (for multiple files use getInstances()) before validate:

$this->image = UploadedFile::getInstance($this, 'image');
like image 65
arogachev Avatar answered Oct 11 '22 09:10

arogachev


you can also set image dimension using this along with max file size

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

this allows you to upload maximum of 2mb with 250px width and 250px height

like image 27
Shuhad zaman Avatar answered Oct 11 '22 10:10

Shuhad zaman