Reputation: 4690
I'm using ajax request to send an image to server and returning immediately some data. (for test)
return [
'photo' => Request::file('avatar'),
'hasFile' => Request::hasFile('avatar'),
'extension' => Request::file('avatar')->getClientOriginalExtension(),
'size' => Request::file('avatar')->getClientSize(),
'maxFileSize' => Request::file('avatar')->getMaxFilesize()
];
When image is less than upload_max_filesize directive, i have this response:
{"photo":{},"hasFile":true,"extension":"jpg","size":900498,"maxFileSize":2097152}
When image is greater than upload_max_filesize directive, i have this response:
{"photo":{},"hasFile":false,"extension":"jpg","size":0,"maxFileSize":2097152}
Look at size
on last response. How i can validate it?
Upvotes: 0
Views: 1346
Reputation: 4557
You need to use size instead of max to validate file size. max:value
Blockquote The field under validation must be less than or equal to a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule So we go to the Size rule:
size:value
Blockquote The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.
get the file size
$filesize = Input::file('photo')->getSize();
push the file size in the data to be validated
$data['filesize'] = $filesize;
your rule should look like this $rules = ['filesize' => 'size:max size in KB'];
you can also do this when the file size == false, before call the form validation
$file_size Input::file('avatar')->getSize() // false
if ($filesize){
// continue
}
I think this approach is a lot easier.
Hope this help.
Upvotes: 2
Reputation: 4302
As I know you could be able to validate a type of image by client side script but in case of size its completely server side validation .
Upvotes: 0