How to Fix PostTooLargeException in Laravel on Apache Server?




How to Fix PostTooLargeException in Laravel on Apache Server?

Hi dev,

If you have ever attempted to upload a file greater than 2MB in your journey as a Laravel developer, you have probably come across a stunning screen like to the one in the figure above.

This is a fairly little article, but after going through it multiple times, I felt the need to write the solution for both myself and anyone else who might need it in the future. Go there with me!

Run the following

$ php -i | grep php.ini

The command above will print the path to the php.ini file that your server is using, in my case I got:

Configuration File (php.ini) Path => /etc/php/7.2/cli

Loaded Configuration File => /etc/php/7.2/cli/php.ini

Before now, you might have been editing /etc/php/7.2/apache2/php.ini thinking that would solve the problem but no, it does not!

Now run:

$ sudo nano /etc/php/7.2/cli/php.ini

Our concern is the values of post_max_size, upload_max_filesize and memory_limit which by default has the value of 8M, 2M, and 128M respectively. Now you see why you could not upload more than 2MB sized file

Next up, search for those variables and change their values to suit your need. While doing that, ensure that the sizes follow the same ratio as the default values, that is memory_limit should be greater followed by post_max_size and then upload_max_filesize. For example, in my case, I used the following values:

post_max_size = 6G

upload_max_filesize = 4G

memory_limit = 8G

Once you’re done changing the values to suit your need, save the changes with CTL Xthen type Y and hit ENTER.

Next up, restart apache:

$ sudo /etc/init.d/apache2 restart

Then restart your PHP server with the usual

php artisan serve