Check the documentation
File Validation - How to set a max file size?
Hi,
I would like to have some sort of validation so the user can not upload more than a max file size. Ive already added this setting in Apache but i want something the client can see.
thanks
'hero_image' => 'required|mimes:jpeg,bmp,png|size:5000',
Even if i add this validation of 5mb and try to upload a lot more i get the error.
TokenMismatchException in VerifyCsrfToken.php line 67:
When i try to save/upload.
Your missing the token {{ csrf_field() }} under your form element.
This error is not to do with missing token. It's to do with apache trying to upload a file.
its giving me that because the file size i am trying to upload is greater than the apache upload_max_file_size.
If i upload one which is smaller than the apache max file size it posts fine. Thats why i don't think its actually got anything to do with the token, that and i can see the token in inspect element.
in my log when i try to post "PHP Warning: POST Content-Length of 1457522843 bytes exceeds the limit of 8388608 bytes in Unknown on line 0"
than you must increase upload_max_file_size in your php.ini
Im not sure why i would do that when i want to restrict the max file size?
What im trying to say is i get this error rather than the laravel validation error.
I think that server cant handle request if file exceeds upload_max_file_size ...
So my upload_max_file)size = 150mb
Im trying to upload a image which is 10mb
my validation rule is set to 5000 kilobrytes (5mb)
but i still get that weirdtoken mismatch error.
Make sure post_max_size is also set correctly (could fail if you send more data than allowed here) and that your form tag has attribute enctype="multipart/form-data"..
Awesome it works now but i have a small issue wiht it
"The hero image must be 5000 kilobytes."
is it possible i can change this error message so that its megabytes so its easier for the end user to understand.
Documentation should help: https://laravel.com/docs/5.2/validation#custom-error-messages
There's an example in their for validating filesize and giving a custom error message :)
@KHAN - you can use max instead of size
'hero_image' => 'required|mimes:jpeg,bmp,png|max:5000',
when you use size, you must upload files of size 5000kb
This is such an old post, but as it has been dragged up...
If the upload exceeds the max post size then other form data is never recieved. Since the csrf token is checked before any validation the missing token error is seen.
I believe this has been fixed in Laravel releases since 2016
@SNAPEY - Well, I'm glad you dragged it up - that's a really good point. Thanks!
Please or to participate in this conversation.