25

I am getting the post too large exception when I try to upload files which are more than 2 MB in size and I've already tried everything like increasing the post_max_size and max_upload_size in php.ini file but I am still getting the same error. When I comment the handler function in validatepostsize.php file it shows token mismatch error:

5
  • 1
    Did you restart the web server and/or php after changing the setting?
    – markdwhite
    Apr 18, 2017 at 6:08
  • Yes i tried restarting both apache and laravel loacal development server but still getting the error Apr 18, 2017 at 6:30
  • 1
    Did you verify that your changes are in effect? Try doing a phpinfo() and search for post_max_size. Apr 18, 2017 at 8:09
  • Allthough you're not getting a 413, don't forget to check the max request size in the webserver's config (apache: LimitRequestBody / nginx: client_max_body_size). On apache it's defaulted to 0 but nginx does have a restricted value set on default.
    – trommelaap
    Apr 18, 2017 at 8:36
  • I changed post_max_size in php.ini file and it shows in phpinfo() that changes that I made are in effect but still getting the same error. Apr 18, 2017 at 12:25

6 Answers 6

31

I think you can check the following parameters in your php.ini file like:

upload_max_filesize = 40m
post_max_size = 50m

After restart your server like (apache)

Hope this work for you!

2
  • Keep in mind that if you change the php.ini on your machine to make your code work, you will also have to do it in the production environment. If possible, a better approach would be to reduce post size for example by compression (if you use images). Jun 11, 2022 at 0:21
  • On Windows, I've set: post_max_size = 2G, upload_max_filesize = 1G and memory_limit = 3G to C:\wamp64\bin\apache\apache2.4.46\bin\php.ini and C:\wamp64\bin\php\php8.1.6\php.ini, then, I restarted Wamp but it still doesn't work. I still get this error: Illuminate \ Http \ Exceptions \ PostTooLargeException.
    – Alex01
    Jul 28 at 6:08
12

First, check your PHP version.

php -v

The command below will print the path to the php.ini file that your server is using.

php -i | grep php.ini

Next.

sudo nano /etc/php/7.4/cli/php.ini

The values of post_max_size, upload_max_filesize and memory_limit by default have the value of 8M, 2M, and 128M respectively.

Search for those variables and change their values, whilst ensuring that the sizes follow the same ratio as the default values.

See example below:

post_max_size = 2G
upload_max_filesize = 1G
memory_limit = 3G

For "heavy-loaded sites", it is useful to have FPM installed and running.

sudo apt install php7.4-fpm -y
sudo service php7.4-fpm start

Finally, restart your web server.

sudo service apache2 restart
3
  • Whats is the path to the php.ini file that my server is using on Windows 11 and not Linux?
    – Alex01
    Jul 28 at 5:51
  • @Alex01 it would differ based on the development platform you are using, such as WAMP/XAMPP/Laragon, etc... Jul 28 at 7:30
  • For me it's Wamp
    – Alex01
    Jul 28 at 9:57
11

One of the issues is that the Laravel PHP artisan serve local development server uses the php.ini under PHP/CLI instead of under PHP/apache2

So if you are having this issue while developing with PHP artisan serve, go and fetch the php.ini that's under the /etc/php[version]/cli folder.

0
3

Its not the best solution just to change parameters in php.ini and then restart apache:

upload_max_filesize = 128M
post_max_size = 128m

//or whatever value you think is right

Restart apache like this: sudo service apache2 restart

The best way as for me is to catch PostTooLargeException using Error Handling of laravel: Error Handling

For a detailed answer how to catch PostTooLargeException using Error Handling you can see my answer in this thread: thread with full answer.

Happy coding!

1

Actually I am looking for the same answer for the same issue. I have found out that the issue can be resolve with an edit of .htaccess file.

Add the below code in the file.

php_value upload_max_filesize 50M
php_value post_max_size 50M

This above two line will resolve the issue but if you are adding this much size then the line below give will save you from future issue. of like execution time expire and all.

php_value max_input_time 120
php_value max_execution_time 120
0

For a more accurate validation, I recommend setting the values in bytes, because 50 MB is 52428800B, which actually 52 MB, not 5000000B.

.htaccess example:

php_value upload_max_filesize 50000000B // 50M
php_value post_max_size 50000000B // 50M

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.