How to Fix PostTooLargeException in Laravel on Apache Server

PostTooLargeException

In your journey as a Laravel developer, if you have ever tried uploading a file larger than 2MB then you’d have encountered a beautiful screen like the one shown in the figure above.

Before I dive into showing you how to fix the error above, this article assumes that the reader has a Laravel app running and only needs to fix the error 😐

This article is a very short one but after several times of experiencing it, I felt I should document the solution for myself and for those who may as well have need of it. Let’s go there!

Note: the following commands are based on Linux environment, if it does not work for your OS, you can find the equivalent command for your OS.

The steps are as follows:

  • open your terminal [CTL + SHIFT t]
  • Run the command below:
$ 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 usualphp artisan serve.

How to fix it in Nginx (Linux)

  • $ sudo nano /etc/php/7.2/fpm/php.ini
  • Edit the values as stated above, remember to replace 7.2 with the version of php you’re using.
  • $ sudo systemctl restart php7.2-fpm

How to fix it in Nginx (Mac)

  • $ sudo cp /etc/php.ini.default /etc/php.ini
  • Edit the values as stated above
  • brew services restart php@7.2 [ replace 7.2 with the version of php you’re using]

That is it! Now you can upload large files within the range of the values you specified above without troubles 😄.

Conclusion

In this short article, if you followed it up to this point, you’d have learned how to know the exact php.ini that your Laravel application uses at runtime. You’d have also learned how you can change the values that will enable you to upload large files as you may need in your application.

If this article was helpful, then I am glad to have helped. You can encourage me by hitting the 👏 icon and also sharing with your friends. For more updates and contributions, kindly follow me here or on twitter. Cheers! 😄

Orie Chinedu Emmanuel

Written by

Software Engineer | Tech Lover