DEV3 Technologies home

DEV3 Developer Forum

Why can't I upload files larger than 2 MB in PHP and how do I change the limit?

PHP helpSolved0 views
dev3 ·

My upload form works with small images but a 6 MB PDF returns an empty $_FILES and $_POST. I changed upload_max_filesize in php.ini but nothing changed. What settings control this?

#php#php-ini#upload

0

1 reply

dev3 · Accepted answer

Several limits apply together. The smallest one wins.

PHP settings

Setting Meaning Default
upload_max_filesize max size of one file 2M
post_max_size max size of the whole request body 8M
max_file_uploads max files per request 20
memory_limit only matters if you process the file in memory 128M
max_execution_time, max_input_time slow uploads on weak connections 30 / 60

post_max_size must be larger than upload_max_filesize. If the request exceeds post_max_size, PHP drops everything, so both $_FILES and $_POST come back empty. That is the symptom you see.

Where to change it

  1. Find the active file: php -i | grep "Loaded Configuration" for CLI, but for the web use phpinfo() or php_ini_loaded_file() in a script served by the web server (PHP-FPM and CLI often use different ini files).
  2. Edit php.ini, restart PHP-FPM or Apache.
  3. Shared hosting: use "MultiPHP INI Editor" in cPanel, or a .user.ini file in the web root (PHP-FPM/CGI), or php_value upload_max_filesize 20M in .htaccess (only mod_php). Wrong directives in .htaccess under FPM cause a 500 error.
upload_max_filesize = 20M
post_max_size = 25M

Web server limits

  • Nginx: client_max_body_size 25m; (default 1m returns 413).
  • Apache: LimitRequestBody.
  • Cloudflare free plan limits request bodies to 100 MB; reverse proxies may have their own cap.

Check the error in code

if ($_FILES['doc']['error'] !== UPLOAD_ERR_OK) {
    // UPLOAD_ERR_INI_SIZE (1) exceeded upload_max_filesize
    // UPLOAD_ERR_FORM_SIZE (2) exceeded MAX_FILE_SIZE field
}

Verify the active values at runtime with ini_get('upload_max_filesize'). See also the .htaccess generator and nginx config generator.

0

Your reply

Every post is reviewed by DEV3 before it appears. Usually reviewed within 24 hours.

Markdown supported. Use ``` for code blocks. Images are not allowed.