Reader Stacks

How to Increase the Maximum File Upload Size in PHP

Three separate php.ini directives all cap upload size independently — raising just one and leaving the others at their default is the most common reason this fix doesn't seem to work.

PHP's maximum upload size is actually controlled by three separate php.ini directives that all need to be raised together — changing only one and leaving the others at their smaller default is exactly why this fix often seems not to work on the first attempt.

The three directives that all need to agree

; php.ini
upload_max_filesize = 20M
post_max_size = 25M
memory_limit = 128M

upload_max_filesize caps an individual uploaded file's size, post_max_size caps the entire POST request body (which must be at least as large as upload_max_filesize, since the file is part of that body), and memory_limit needs enough headroom to actually process a request that size — set post_max_size a bit higher than upload_max_filesize to leave room for the rest of the form's other fields.

Finding the actual php.ini file being used

php --ini

This shows exactly which php.ini file the currently active PHP CLI/version is loading — a server can have multiple PHP versions installed, each with its own separate ini file, and editing the wrong one is a common reason a change appears to have no effect.

Restarting the web server or PHP-FPM after the change

sudo systemctl restart php8.4-fpm
sudo systemctl restart nginx
# or, for an Apache + mod_php setup
sudo systemctl restart apache2

php.ini is read once when PHP starts — a restart of the appropriate service (PHP-FPM for an Nginx setup, or Apache for mod_php) is required before a changed value actually takes effect, and this restart step is the other common thing people forget.

Overriding these values per-directory with .htaccess (Apache with mod_php only)

php_value upload_max_filesize 20M
php_value post_max_size 25M

This only works with Apache running PHP as a module (mod_php) — it has no effect with PHP-FPM, which is the more common setup with Nginx, or increasingly with Apache too via mod_fpm.

Setting these values at runtime, per-request, when server config access isn't available

ini_set('upload_max_filesize', '20M'); // does NOT work — this directive can only be set in php.ini

upload_max_filesize and post_max_size are both flagged as PHP_INI_PERDIR in PHP's configuration system, meaning they genuinely cannot be changed at runtime with ini_set() — they require actual php.ini (or, on shared hosting, sometimes a control-panel setting) access, which is worth knowing before spending time debugging why a runtime ini_set() call for these specific two directives isn't working.

Laravel-side validation, separate from the php.ini limits

$request->validate([
    'document' => 'required|file|max:20480', // max is in kilobytes
]);

Laravel's own max validation rule is a separate, application-level check — raising the php.ini limits doesn't do anything on its own if the validation rule's max value is still set lower than the file actually being uploaded.