0

I currently have a form with a file upload, and on that file upload I have a validation that makes sure it's an image or document and also makes sure the size can't be any larger than 2M :

'insurence_document' => 'required|mimes:jpeg,png,jpg,pdf,doc,docx:|max:2048',

But I don't understand, the validation rules I have set should stop it from even getting it's a .zip! It's not even the correct mime type. But if I upload a smaller .zip, it validates fine. So the issue seems to be with larger files.

I error like Warning:

POST Content-Length of 24479807 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

Its not aboutthe above error, it's about laravel validation not work with large upload files. I want file size maintain like 2MB only.

I want it done by laravel validation only

venky
  • 73
  • 1
  • 1
  • 9
  • Did you add `{{ csrf_field() }}` to your form? – Ajay Makwana Mar 13 '19 at 06:28
  • You've written `docx:`, I'm guessing this breaks the validation. – Linus Juhlin Mar 13 '19 at 06:30
  • yes I add. it's not about csrf_field() , it's about if file too large when i upload I can't get validation errors – venky Mar 13 '19 at 06:31
  • no problem with That. But if I upload a smaller .zip, validations fine – venky Mar 13 '19 at 06:34
  • Yes! But the error `TokenMismatchException in VerifyCsrfToken.php line 68:` is about CSRF only. That's why I asked. – Ajay Makwana Mar 13 '19 at 06:35
  • https://stackoverflow.com/questions/11719495/php-warning-post-content-length-of-8978294-bytes-exceeds-the-limit-of-8388608-b – Ajay Makwana Mar 13 '19 at 06:46
  • Possible duplicate of [PHP Warning: POST Content-Length of 8978294 bytes exceeds the limit of 8388608 bytes in Unknown on line 0](https://stackoverflow.com/questions/11719495/php-warning-post-content-length-of-8978294-bytes-exceeds-the-limit-of-8388608-b) – Linus Juhlin Mar 13 '19 at 06:50
  • it's not about the size 2MB , it's about laravel validation won't work if i upload larger files. if i upload smaller files validations work. @LinusJuhlin – venky Mar 13 '19 at 07:05

4 Answers4

0

8388608 bytes is 8M, Default file upload size is 2MB

Open the php.ini file. Find these lines in the php.ini file and replace it following numbers: upload_max_filesize = 64M Save the changes and try uploading the file again.

You can find the path in your xampp/php/php.ini(Windows User) file And must restart your server

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • it's not about the size 2MB , it's about laravel validation won't work if i upload larger files. if i upload smaller files validations work. – venky Mar 13 '19 at 07:03
  • @venky Have you tried my answer? please try and let me know – Udhav Sarvaiya Mar 13 '19 at 07:08
  • I want to work without change upload_max_filesize for laravel validation , okay if I change after that i upload more than 64MB file means same error I got right. – venky Mar 13 '19 at 07:12
  • 1
    @venky This won't work. Before any file comes to laravel (and his validation) it's already being rejected by php.ini configuration. This is how php works. P.S. you probably would like to increase max_post_size property in php.ini as well. – arku Mar 13 '19 at 10:29
0

I would create a custom Laravel validation rule for this, to figure out why this passing the Zip file and the 2mb filesize. There you would be able to handle manually the validation and see why passes the rules you want to set. If you figure it out, I'm sure you'll be able to find out why this happens.

Zoli
  • 1,081
  • 1
  • 8
  • 28
  • it's not about only zip file, I tried with different formats , if it's large size file i get error page, it's not validate like laravel validation msg it's more than 2MB file. – venky Mar 13 '19 at 09:06
0

Use upload_max_filesize = -1 in you php.ini file

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Kumar Subedi
  • 334
  • 3
  • 8
0

If you exceed the php limit then the server will refuse the upload before it reaches your validator. This way it will not produce the validation message but php will throw an error. So even that the file type is different from mime you set up on rule, you will not get the message of that rule because it does not reach it.

Another case is if you set required on rule and the file exceed the php limit than you will receive the message from validaiton: The field is required. This happens for the same reason mentioned before, since the file does not reaches the validator, the validator will look at the input as an empty field and will throw the required message.

Solution is to change post_max_size and upload_max_filesize in your php.ini.

Happy coding!

Edit:: If you want to catch the error generated from php when you exceed the limit of post_max_size and upload_max_size you can see my answer: here.

Marinario Agalliu
  • 989
  • 10
  • 25