In this guide, we will walk you through how to increase the maximum file upload size in PHP. The best way to modify the maximum file upload size is to set the values in the php.ini file.
There are two directives in the php.ini file that you will need to change which are:
- upload_max_filesize: This is the maximum size of an individual uploaded file. The default value for this is 2MB.
- post_max_size: This is the maximum size that a POST request can be. This value must be equal to or larger than the value specified for
upload_max_filesize. It is best practice to set this higher to account for the overall size of the post request. The default value for this directive is 8MB.
To increase the maximum file upload size to 64MB in PHP for example you would enter the directives as follows:
upload_max_filesize = 64M post_max_size = 96M
For the changes to take effect you then need to restart your web server.
Versions prior to PHP 5.3 these values could be set via the ini_set PHP function at runtime. However, newer versions of PHP will not allow you to modify these values via the ini_set function because the two directives involved are PHP_INI_PERDIR. Learn more here.
Being PHP_INI_PERDIR you can create a
.user.ini file, add the above values for upload and post max sizes and place it in the same directory as the upload script.
Just for reference, you can use the ini_set function like so in your PHP script:
ini_set( 'upload_max_filesize', '64M' ); ini_set( 'post_max_size', '96M' );
Increase Maximum File Upload Size Using .htaccess File
If your server configuration does not restrict you from overriding PHP values using a .htaccess file, then you can do this:
<IfModule mod_php7.c> php_value upload_max_filesize 64M php_value post_max_size 96M </IfModule>
There you have it! You now know how to increase the maximum file upload size in PHP. We recently published a guide showing you how to increase file upload size in Apache here.