Skip to the fix
PHPSolvedThe fix first, then the reason

Newest fixes

Nomvula Sithole in PHP, phpMyAdmin | | 7 min

The uploaded file exceeds the upload_max_filesize directive in php.ini: fix

Set upload_max_filesize, a larger post_max_size and the web server's body limit, then restart PHP-FPM. The full chain, with a script that shows what applies.

A large parcel stuck in the letterbox of a wooden door
On this page
  1. See what applies before changing more
  2. The chain a file passes through
  3. Why ini_set does nothing
  4. Which file wins
  5. When it's a 413 and not a PHP error
  6. Slow uploads and temporary space
  7. phpMyAdmin imports
  8. How far to raise it

Three lines in the php.ini that your web server uses, and one in the web server itself:

ini
upload_max_filesize = 64M
post_max_size = 72M
memory_limit = 128M
nginx
client_max_body_size 72m;

Then restart PHP-FPM (or Apache, if PHP runs inside it) and reload nginx. If the error survives that, one of four things happened: you edited a file that PHP doesn't read, something overrides it, the value has a typo, or a different limit in the chain is the one refusing you. The rest of this page takes them in turn.

See what applies before changing more

Put this in a file inside the site, open it in the browser, and delete it afterwards. Run from the command line it shows the command line's values, which are often different.

phpran on PHP 8.2.8
<?php
function bytes(string $value): int
{
    $value = trim($value);
    $unit = strtolower(substr($value, -1));
    $factor = ['k' => 1024, 'm' => 1024 ** 2, 'g' => 1024 ** 3][$unit] ?? 1;
    return (int) $value * $factor;
}

foreach (['2M', '64M', '1G', '64MB', '0.5M'] as $setting) {
    printf("%-5s %d\n", $setting, bytes($setting));
}

$upload = bytes(ini_get('upload_max_filesize'));
$post = bytes(ini_get('post_max_size'));
echo 'largest file: ', min($upload, $post ?: PHP_INT_MAX) / 1024 ** 2, " MB\n";
echo $post > $upload || $post === 0 ? "post_max_size leaves room\n" : "post_max_size is too small\n";
real output
2M    2097152
64M   67108864
1G    1073741824
64MB  64
0.5M  0
largest file: 2 MB
post_max_size leaves room

The 2 MB at the end is PHP's built-in default, and 8 MB is the default for post_max_size. If your browser shows those numbers after an edit, PHP hasn't read your file.

Look at 64MB too. PHP knows the letters K, M and G, and nothing else, so MB isn't a unit. PHP 8.2 started with that value says so and takes the number alone:

output
Warning: Invalid "upload_max_filesize" setting. Invalid quantity "64MB": unknown multiplier "B", interpreting as "64" for backwards compatibility

That's a limit of 64 bytes, and every upload fails. Decimals are cut as well: 0.5M becomes 0, and 1.5G becomes 1G. The PHP FAQ documents the notation. From PHP 8.2 you can use the built-in ini_parse_quantity() in place of the function above.

The chain a file passes through

StepSettingDefaultWhen it's exceeded
nginxclient_max_body_size1m413 page, PHP never runs
ApacheLimitRequestBody1 GB since 2.4.54Error page, PHP never runs
PHP, whole requestpost_max_size8M$_POST and $_FILES are empty
PHP, each fileupload_max_filesize2MError code 1 for that file
PHP, number of filesmax_file_uploads20Files beyond the limit are ignored
The formMAX_FILE_SIZE fieldnoneError code 2
Reading the requestmax_input_timesee belowSlow uploads are cut off

The default for nginx is in its core module reference, Apache's in the page on core directives, where versions 2.4.53 and older have no limit. Apache takes plain bytes, so 72 MB is LimitRequestBody 75497472.

upload_max_filesize is the limit for one file, and post_max_size is the limit for everything in the request, with all files, all fields and the multipart overhead. The manual asks for post_max_size to be larger than upload_max_filesize, and memory_limit to be larger than post_max_size, generally speaking. If a form accepts five files of 64 MB each, post_max_size has to cover all five.

Two failures that look different

Here are three uploads to a small script on PHP's built-in server with the default limits. The script prints the size of $_POST and $_FILES, and the error code of each file:

bash
php -d upload_max_filesize=2M -d post_max_size=8M -S 127.0.0.1:8099
curl -F "title=test" -F "dump=@small.bin" http://127.0.0.1:8099/
output
# 500 kB
post=1 files=1 length=500317
dump error=0 size=500000
# 3 MB
post=1 files=1 length=3000318
dump error=1 size=0
# 9 MB
post=0 files=0 length=9000317

The 3 MB file breaks the file limit. The form field still arrives, and the file carries error 1, the one this page is named after. The 9 MB file breaks the request limit. Nothing arrives at all, not even the title, and the only trace is in the PHP log:

output
PHP Warning:  POST Content-Length of 9000317 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

A form that comes back empty, a CSRF token that's suddenly "missing", a "please fill in all fields" message after a large upload: all three are this second case. Catch it by name, before any other validation:

phpran on PHP 8.2.8
<?php
function uploadProblem(array $server, array $post, array $files, string $field): ?string
{
    $length = (int) ($server['CONTENT_LENGTH'] ?? 0);
    if (($server['REQUEST_METHOD'] ?? '') === 'POST' && $length > 0 && !$post && !$files) {
        return "request of $length bytes is over post_max_size";
    }
    return match ($files[$field]['error'] ?? UPLOAD_ERR_NO_FILE) {
        UPLOAD_ERR_OK => null,
        UPLOAD_ERR_INI_SIZE => 'file is over upload_max_filesize',
        UPLOAD_ERR_FORM_SIZE => 'file is over MAX_FILE_SIZE in the form',
        UPLOAD_ERR_PARTIAL => 'upload was interrupted',
        UPLOAD_ERR_NO_FILE => 'no file was chosen',
        UPLOAD_ERR_NO_TMP_DIR => 'no temporary folder on the server',
        UPLOAD_ERR_CANT_WRITE => 'could not write to disk',
        UPLOAD_ERR_EXTENSION => 'stopped by a PHP extension',
        default => 'unknown upload error',
    };
}

$request = ['REQUEST_METHOD' => 'POST', 'CONTENT_LENGTH' => '9000317'];
var_dump(uploadProblem($request, [], [], 'dump'));
var_dump(uploadProblem($request, ['title' => 'x'], ['dump' => ['error' => 1]], 'dump'));
var_dump(uploadProblem($request, ['title' => 'x'], ['dump' => ['error' => 0]], 'dump'));
real output
string(46) "request of 9000317 bytes is over post_max_size"
string(32) "file is over upload_max_filesize"
NULL

In the real script you'd pass $_SERVER, $_POST and $_FILES. All eight values are on the manual page about upload errors. MAX_FILE_SIZE is only a hint to the person filling in the form. Anyone can remove the hidden field, so it never replaces the server's check.

Why ini_set does nothing

Calling ini_set('upload_max_filesize', '64M') at the top of the script is the fix that gets copied most, and it can't work:

phpran on PHP 8.2.8
<?php
var_dump(ini_set('upload_max_filesize', '64M'));
echo ini_get('upload_max_filesize'), "\n";
real output
bool(false)
2M

PHP receives the file before your first line runs, and the setting has the mode INI_PERDIR. The manual says such settings may be set in php.ini, .htaccess, httpd.conf or .user.ini, and not from a script. The same goes for post_max_size and max_input_time. memory_limit and max_execution_time can be changed at run time.

Which file wins

Open a page with phpinfo() through the web server and look at two lines: "Loaded Configuration File", and "Additional .ini files parsed". A later file in that second list can repeat the setting and win. There's a longer walk through this in find the php.ini that PHP reads.

Then the places that override it:

  • A PHP-FPM pool. A line such as php_admin_value[upload_max_filesize] = 2M in the pool file beats php.ini. Settings made with php_admin_value can't be overridden from a script at all. Search the pool directory for upload_max_filesize and post_max_size.
  • A .user.ini. With PHP-FPM or CGI, PHP looks for this file from the folder of the script up to the document root. It's the right tool on shared hosting. Two catches: PHP re-reads it only every 300 seconds by default, and if PHP runs as an Apache module it's ignored.
  • .htaccess. php_value upload_max_filesize 64M works when PHP is an Apache module. With PHP-FPM behind Apache, use .user.ini.
  • nginx. A fastcgi_param PHP_VALUE line can carry its own limits. Search the server block for it as well.

A .user.ini for an upload folder looks like this:

ini
upload_max_filesize = 64M
post_max_size = 72M

To try a value without touching any file, give it to the command line. This affects that one process only:

bash
php -d upload_max_filesize=64M -d post_max_size=72M \
  -r 'echo ini_get("upload_max_filesize"), " / ", ini_get("post_max_size"), PHP_EOL;'
output
64M / 72M

Restart the thing that read the file

PHP reads php.ini when it starts. With PHP-FPM that's the FPM service, and reloading nginx or Apache alone leaves the old values in place. With PHP as an Apache module it's Apache itself. The service name on Debian and Ubuntu contains the PHP version, so list it first:

bash
systemctl list-units --type=service 'php*'
sudo systemctl restart php8.2-fpm
sudo nginx -t && sudo systemctl reload nginx

Several PHP versions side by side means several services and several php.ini files. The version at the top of the phpinfo() page is the one to restart. Only .user.ini needs no restart, just patience for the 300 seconds.

When it's a 413 and not a PHP error

An error page that says "413 Request Entity Too Large" was written by the web server. PHP wasn't asked, so there's no line in the PHP log, and no setting in php.ini will change it.

In nginx, client_max_body_size may stand in the http, server or location block, and the most specific one counts. A 72m in http is useless if the PHP location has a forgotten 8m. Print the whole configuration as nginx sees it, with file names, and search that:

bash
sudo nginx -T | grep -n -E 'configuration file|client_max_body_size'

Apache's LimitRequestBody can sit in the server configuration, a virtual host, a directory section or an .htaccess file. A proxy in front of the server can have a limit of its own, and then your logs show no trace of the request at all. Cloudflare, for one, documents an upload limit per plan, 100 MB on the free one, and answers 413 itself. To find out which layer speaks, send the file to the machine directly and read the headers of the answer:

bash
curl -s -o /dev/null -D - -F "dump=@large.bin" http://127.0.0.1/upload.php -H "Host: www.example.com"

Slow uploads and temporary space

max_input_time limits the time spent receiving and parsing the request. PHP's built-in default is -1, which means that max_execution_time is used, and the php.ini files that ship with PHP set 60. A 64 MB file on a 5 Mbit/s line needs about 110 seconds. If small files pass and large ones stop after a constant time, this or a proxy timeout is what you're looking at.

During the upload the file is kept in upload_tmp_dir, or in the system's temporary folder if that's not set. A full disk gives error 7, and a missing folder error 6. Check free space there, and not only where the file finally goes.

phpMyAdmin imports

phpMyAdmin shows this error under its import button, because an import is an upload like any other. The same chain applies. Its FAQ names max_execution_time, upload_max_filesize, memory_limit and post_max_size as the first things to check. For anything large there are better routes than a 2 GB PHP limit:

  1. Compress the file. The import form accepts compressed dumps, and SQL text shrinks well.
  2. Use UploadDir. Set $cfg['UploadDir'] = '/var/lib/phpmyadmin/upload'; in config.inc.php, copy the dump there with scp or SFTP, and choose it from the list on the Import tab. No HTTP upload takes place. The configuration manual describes it. Keep that folder outside the web root.
  3. Skip the browser. With shell access, this has no PHP limits at all:
bash
mysql -u root -p shop < dump.sql

Importing over an existing database replaces tables if the dump contains DROP TABLE, so make a dump of the target first. If the command line import stops with a lost connection, the limit you've met is MySQL's own packet size, and that one is explained in MySQL server has gone away.

How far to raise it

Set the limit to what the site needs, not to 2G. Every request may send a body up to that size, and PHP has to receive it before your code can refuse it. If one admin form needs more than the rest, give the larger limit to that folder with a .user.ini, or to that location in nginx, and leave the global values low.

Same subject