简体   繁体   中英

Upload file with laravel5.4 file size

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

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

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.

php.ini文件中使用upload_max_filesize = -1

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM