PostTooLargeException in Laravel: Why php.ini Isn't Enough
When you encounter a PostTooLargeException in Laravel while uploading files exceeding 2 MB, the instinctive reaction is to check php.ini. You increase upload_max_filesize and post_max_size, restart the server, and yet the error persists. This frustration is common because PHP handles request size limits at multiple layers, and adjusting configuration in a single place rarely solves the problem unless every layer is aligned correctly.
The error often originates not from PHP itself, but from Nginx or Apache acting as a reverse proxy. If your application server (like PHP-FPM) is configured to accept large uploads, but Nginx drops the request before it reaches PHP, you will still see PostTooLargeException in your Laravel logs. Nginx has its own directive called client_max_body_size. By default, this is often set to 1MB. If you don't explicitly increase this value in your Nginx configuration file (nginx.conf or a site-specific config), the request is rejected immediately, and PHP never gets a chance to process it.
To resolve this, open your Nginx configuration for the relevant server block. Look for the location / or location /upload directives. You need to add or modify the client_max_body_size directive:
location / {
client_max_body_size 10M;
}
After making this change, reload Nginx rather than restarting it to apply the new settings without dropping active connections:
sudo nginx -t && sudo systemctl reload nginx
If you are using Apache instead of Nginx, the equivalent directive is LimitRequestBody. You must add this to your .htaccess file or the virtual host configuration in httpd.conf:
<Directory /var/www/your-project>
LimitRequestBody 10485760
</Directory>
Restart Apache after applying these changes.
Once the web server limits are addressed, you must ensure PHP itself can handle the data. In php.ini, both upload_max_filesize and post_max_size must be equal to or greater than your largest expected file. Note that post_max_size acts as a ceiling for the entire POST request; if you set upload_max_filesize higher than post_max_size, PHP will still truncate the data or throw an error.
upload_max_filesize = 10M
post_max_size = 10M
Restart your PHP-FPM service to apply these changes:
sudo systemctl restart php-fpm
There is another critical layer that often gets overlooked: the framework's own validation logic. Laravel includes a middleware or helper functions designed to cap file uploads, sometimes defaulting to 2 MB regardless of server settings. This is frequently found in files like validatepostsize.php or within the Config\BroadcastChannel setup in older versions, though modern Laravel versions handle this via service providers or custom validation rules.
If you comment out a handler function in a file named validatepostsize.php, you might see a "token mismatch" error. This usually indicates that your frontend is sending a CSRF token that isn't being validated before the size check occurs. However, if the size check itself is hardcoded in a service provider or a specific middleware class, simply commenting it out bypasses security checks and breaks the form submission flow.
Instead of disabling validation globally, adjust the limit within Laravel's configuration using the max_file_size property if you are using the Laravel\MediaLibrary package, or define your own rule in the request validation:
$request->validate([
'file' => 'required|file|max:10240', // 10MB limit
]);
Finally, ensure that your .env file does not have conflicting settings. While php.ini takes precedence over environment variables for these specific directives in most setups, it is good practice to verify that APP_ENV isn't pointing to a configuration file with stricter defaults.
By systematically aligning Nginx/Apache limits, PHP directives, and Laravel's validation logic, you can eliminate the PostTooLargeException without disabling critical security features. If you require further assistance with configuring your specific stack or integrating file upload handling into your Laravel architecture, resources such as https://laravelcompany.com can provide additional guidance on advanced deployment strategies.