如何验证Laravel中的最大文件大小? [英] How to Validate on Max File Size in Laravel?
问题描述
我正在尝试在Laravel中验证最大文件大小为500kb:
I'm trying to validate on a max file size of 500kb in Laravel:
$validator = Validator::make($request->all(), [
'file' => 'size:500',
]);
但这表示文件应恰好为500kb.如何编辑此规则,以使其在大于500kb时返回错误?
But this says that the file should be exactly 500kb big. How can I edit this rule so that it returns an error when it's bigger than 500kb?
我已经尝试过:
'file' => 'size:>=500'
'file' => 'size:max:500'
文档对此没有说明:
大小:值
正在验证的字段必须具有与给定值匹配的大小. 对于字符串数据,该值对应于字符数.为了 数字数据,该值对应于给定的整数值.对于文件, size对应于文件大小(以千字节为单位).
The field under validation must have a size matching the given value. For string data, the value corresponds to the number of characters. For numeric data, the value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.
推荐答案
根据文档:
$validator = Validator::make($request->all(), [
'file' => 'max:500000',
]);
该值以千字节为单位. IE. max:10240
=最多10 MB.
The value is in kilobytes. I.e. max:10240
= max 10 MB.
这篇关于如何验证Laravel中的最大文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!