yii2 – 如何验证文件上传最大大小500KB

前端之家收集整理的这篇文章主要介绍了yii2 – 如何验证文件上传最大大小500KB前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何验证上传文件大小最多500Kb?
我做得很好,但它没有成功:
public function rules()
    {
        return [
...
           'myfile'
            ],'file','extensions' => 'pdf,jpg','maxSize' => 4096000,'tooBig' => 'Limit is 500KB' ],];
    }
您指定了错误的maxSize.

来自官方文档:

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千字节是500 * 1024字节= 512 000字节.

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

您也可以将其指定为’maxSize’=> 500 * 1024,这更具可读性,您无需进行任何计算(对于更复杂的度量单位,这是更好的选择).

有用的链接

> What is kilobyte?
> FileValidator $maxSize

猜你在找的PHP相关文章