Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How validate file upload max size 500KB

Tags:

yii2

How validating the upload files size by up to 500Kb? I'm doing well, but it's not working out:

public function rules()
    {
        return [
...
           'myfile'
            ], 'file', 'extensions' => 'pdf, jpg', 'maxSize' => 4096000, 'tooBig' => 'Limit is 500KB' ],
        ];
    }
like image 205
gugoan Avatar asked May 13 '15 14:05

gugoan


People also ask

How do I change the upload max file size?

Using Plugin MethodGo to your WordPress Dashboard → Plugins → Add new, search “Increase Max Upload Filesize”, then Install and Activate the plugin. Once installed, go to plugin settings and simply enter the value for upload size. Click the Save Changes button to apply the new upload size.


1 Answers

You specified wrong maxSize.

From offical docs:

The maximum number of bytes required for the uploaded file. Defaults to null, meaning no limit. Note, the size limit is also affected by 'upload_max_filesize' INI setting and the 'MAX_FILE_SIZE' hidden field value.

See also $tooBig for the customized message for a file that is too big.

500 kilobytes is 500 * 1024 bytes = 512 000 bytes.

public function rules()
{
    return [
        ['myfile', 'file', 'extensions' => 'pdf, jpg', 'maxSize' => 512000, 'tooBig' => 'Limit is 500KB'],
    ];
}

Also you can specify it like 'maxSize' => 500 * 1024, this is more readable and you don't have to do any calculations (for more complex measure units this is preferable option).

Useful links:

  • What is kilobyte?
  • FileValidator $maxSize
like image 146
arogachev Avatar answered Sep 19 '22 21:09

arogachev