Image file validation - max size issue
See original GitHub issueWhen trying to limit image uploads to 10mb using the image field, I get this error message on a 4.2mb image:
The photo may not be greater than 10000 characters.
Assuming this is because its validating the base64 encoded string, not the original file size.
Here’s my validation rules:
return [
'name' => 'required|max:255',
'photo' => 'max:10000'
];
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
max file size validation for file/image fields - ProcessWire
In my imagination the file/image field get a new option: max file size. The validation (simple JS check if the file selected for...
Read more >Laravel file size validation returns error regardless of file size
size:value => The field under validation must have a size matching the given value. Use max instead of size
Read more >File size validator should only respect the explicitly configured ...
Problem /Motivation Drupal\file\Plugin\Field\FieldType\FileItem::getUploadValidators() always sets the maximum file size for uploading based ...
Read more >Validation of file size while uploading using JavaScript / jQuery
In this article, we will learn how to implement file size validation by checking file size before uploading using Javascript and jQuery.
Read more >Content validation | Uploadcare Docs
Limit File Size · Restrict File Type · Image Dimensions · Image Orientation. File Size. The example below shows how to add validation...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So, if anyone else is looking for a solution to validate the file size of the image, I made the following.
Just add a new Validation to
AppServiceProvider.php
:Note that I’m using Intervention for checking it is an image (I had intervention already). Then, on the validator parameter I pass the max file size (on kb), as follows:
Cheers.
According to docs, max:value
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.
size:value
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 an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.
It seems correct from the docs, but docs refer to uploaded files/images, not base64 encoded strings, I think you will need a custom validator here. Refer to https://laravel.com/docs/5.4/validation#custom-validation-rules and try something like this
And then your custom rule: