I got this question at least a dozen times: “I’m raising my file validation rule in to 20 MB but still get errors, it doesn’t work”. So decided to expand on it and explain the reason, and what to do.

First, let’s see how to validate file in Laravel. In your Form Request files or validate() method you can pass array with this parameter:

The last part means that size should be not more than 20 MB (20000 kB). But that may be not enough, cause file restrictions exist not only on Laravel application level.

PHP settings in php.ini

There are two settings related to size in php.ini file. Here they are with their default values:

As you can see, by default you can upload files only up to 2 MB, so you should change that to 20M. But even that won’t allow you to upload 20 MB files, because of overall POST request restriction by post_max_size, it should be set to 20M, or rather even bigger than that, cause POST will likely to have more than just the file, so I would set it to 21M at least, in that case.

Also, please make sure you’re editing the correct php.ini file, cause there are cases with multiple files on the same server, for different PHP versions, also for FPM and CLI settings.

But even this may be not enough. There’s also web-server configuration.

Nginx and Apache Settings

By default Laravel Forge creates servers with LEMP stack, which uses Nginx as a web-server. Even without knowing how to configure it, you need to care about one setting in nginx.conf file: client_max_body_size.

Here’s a screenshot from official documentation:

As you can see, default value is only 1m, which means that your whole POST request may be maximum 1MB. So you need to change that setting to 20m or higher.

If you’re using Apache web-server, there’s also a setting for that called LimitRequestBody. Here’s a screenshot from the docs:

The difference here is that Apache doesn’t give any restrictions by default. So there’s a chance that you will never need to edit this value, but just in case, it’s good to know it exists.

So these are my on how to upload bigger files in Laravel. Anything I’ve missed?

Want to generate Laravel adminpanel online?
You don’t need any packages to do that!

Source link
thanks you RSS link
( https://laraveldaily.com/validate-max-file-size-in-laravel-php-and-web-server/)


Please enter your comment!
Please enter your name here