Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

php - How to Validate on Max File Size in Laravel?

I'm trying to validate on a max file size of 500kb in Laravel:

$validator = Validator::make($request->all(), [
    'file' => 'size:500',
]);

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?

Ive tried this:

'file' => 'size:>=500'
'file'  => 'size:max:500'

The documentation says nothing about this:

size:value

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the documentation:

$validator = Validator::make($request->all(), [
    'file' => 'max:500000',
]);

The value is in kilobytes. I.e. max:10240 = max 10 MB.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...