Increase PHP Maximum Upload Size
In this article
If you've ever tried to upload a large file through a PHP-based website or application and hit an error like "The uploaded file exceeds the upload_max_filesize directive," you're dealing with PHP's built-in upload limits. By default, PHP allows uploads of only 2 MB, which is far too small for videos, high-resolution images, or large PDFs. This guide walks you through the exact steps to increase that limit by editing your PHP configuration files. You'll need access to your server's file system and the ability to edit configuration files—if you're on shared hosting, you may be limited to using the cPanel file manager or a custom php.ini file.
What You'll Need
- Access to your server's file system (via FTP, SSH, or a hosting control panel like cPanel or Plesk).
- Ability to edit files like
php.ini,.htaccess, or.user.ini. - Knowledge of where your PHP configuration file is located (use
phpinfo()to find it). - A text editor (Notepad++, VS Code, or even the built-in editor in cPanel).
Step-by-Step
- Find your current PHP configuration. Create a file named
phpinfo.phpwith the content<?php phpinfo(); ?>, upload it to your web root, and open it in a browser. Look for theLoaded Configuration Fileline—that's the path to your mainphp.inifile. Also note the current values ofupload_max_filesizeandpost_max_size. - Decide which configuration method to use. If you have root or administrator access, edit the main
php.inifile. If you're on shared hosting, you may need to use a.htaccessfile (for Apache) or a.user.inifile (for CGI/FastCGI). Choose the one that applies to your server setup. - Edit
php.ini(if you have access). Open thephp.inifile in a text editor. Locate the lines forupload_max_filesizeandpost_max_size. Change them to your desired values, e.g.,upload_max_filesize = 100Mandpost_max_size = 100M. Also checkmax_execution_timeandmax_input_time—increase them to, say, 300 seconds each if you're uploading large files. Save the file. - Edit
.htaccess(if you're on Apache and can't editphp.ini). In your web root or the directory where the upload script resides, open or create a.htaccessfile. Add these lines:php_value upload_max_filesize 100Mandphp_value post_max_size 100M. You can also setphp_value max_execution_time 300andphp_value max_input_time 300. Save the file. - Edit
.user.ini(for CGI/FastCGI). If your server uses PHP-FPM, you can create a.user.inifile in your public_html directory with the same directives:upload_max_filesize=100Mandpost_max_size=100M. This file is often used on shared hosting. - Restart the web server or PHP-FPM. If you edited
php.ini, you must restart Apache or Nginx (and PHP-FPM if used) for changes to take effect. On shared hosting, you may not be able to restart—in that case, changes to.htaccessor.user.iniusually apply immediately, but some hosts require a short wait. - Verify the change. Re-open the
phpinfo.phpfile in your browser and check theupload_max_filesizeandpost_max_sizevalues. If they show your new values, you're done. Delete thephpinfo.phpfile for security.
Common Mistakes
- Only changing
upload_max_filesize. Thepost_max_sizemust be equal to or larger thanupload_max_filesizebecause the file is sent as part of the POST request. Ifpost_max_sizeis smaller, your upload will still fail. - Using the wrong file. Editing
php.iniwhen your server uses.htaccessor vice versa can result in no change. Always check which method your host supports. - Forgetting to restart the server. Changes to
php.iniwon't take effect until the web server or PHP-FPM is restarted. Some hosts require you to do this through a control panel. - Setting values too high. Setting upload limits to 1 GB on a shared server can cause memory issues or be overridden by the host. Also, your server's
client_max_body_size(Nginx) orLimitRequestBody(Apache) can override PHP settings. - Ignoring memory limits. PHP's
memory_limitmust be at least as high aspost_max_sizefor the script to handle the upload. Set it to, e.g., 128M or higher.
Tips
- Use consistent values. A common practice is to set
upload_max_filesizeandpost_max_sizeto the same value, andmemory_limitto twice that value. - Check your web server limits. If you're using Nginx, look for
client_max_body_sizein the server block and increase it. For Apache, checkLimitRequestBodyin the configuration. - Test with a dummy file. After making changes, upload a file of the maximum size you expect to handle to confirm everything works.
- Keep a backup. Before editing
php.inior.htaccess, save a copy so you can revert if something breaks.
Troubleshooting
If the new limits don't take effect, try these checks in order:
- Confirm you're editing the correct file. Re-run
phpinfo()and look at theLoaded Configuration Filepath. If you edited a differentphp.ini, your changes won't apply. - Check for overrides. Some hosting providers set their own limits in a parent configuration file that overrides yours. Look for
php_admin_valuedirectives in Apache config orphp_admin_flagin FPM pools—these cannot be overridden by.htaccessor.user.ini. - Verify syntax. A typo in
php.inican cause the server to ignore the file. Usephp -l /path/to/php.inifrom the command line to check syntax. - Restart everything. If you're using PHP-FPM, restart both FPM and the web server. On some systems, you need to run
sudo systemctl restart php-fpm apache2(or nginx). - Look for other limits. If you're still getting errors, check the server error logs. The error may be from the web server (e.g., Nginx's
client_max_body_size) rather than PHP. - Ask your host. If you've tried everything and it still doesn't work, contact your hosting provider. They may have restrictions in place that you can't bypass.
Frequently Asked Questions
1. What is the default PHP upload limit?
By default, PHP sets upload_max_filesize to 2 MB and post_max_size to 8 MB. These values can be lower on some hosts.
2. Do I need to change both upload_max_filesize and post_max_size?
Yes. The file is sent as part of a POST request, so post_max_size must be at least as large as upload_max_filesize. If you only increase the upload limit, the POST size limit will still block larger files.
3. Can I increase the limit using only a PHP script?
No. You cannot change these directives at runtime using ini_set() because they are PHP_INI_PERDIR settings, which means they must be set in php.ini, .htaccess, or .user.ini before the script runs.
4. Why does my upload still fail after I increased the limit?
Other factors can interfere: the web server's own request size limit (e.g., Nginx's client_max_body_size), the memory_limit in PHP, or the script's execution time. Check all of these.
5. Is it safe to set the upload limit to 1 GB?
It's possible, but it depends on your server's resources. Large uploads consume memory and disk space. On shared hosting, the host may override your settings or reject such high values. It's safer to set a limit that matches your actual needs.
6. How do I find the correct php.ini file?
Create a file with <?php phpinfo(); ?> and open it in a browser. Look for the "Loaded Configuration File" entry—that's the file you need to edit. Delete the file afterward.