How to update upload_max_filesize and post_max_size in PHP examples

update upload_max_filesize and post_max_size

In this article let’s understand how to update upload_max_filesize and post_max_size in PHP with examples.

So while working with PHP we my encounter error about above thing when we want to upload any file or something but our server configuration does not fulfil its size. In this case we will need to update them on server to get started with working on large size files.

To do so there are thee ways we can do it :

  • Using php.ini file
  • Using .htaccess file
  • Using inline php function
  • Using .user.ini

1. Using php.ini file :

We can update these values directly on server in php.ini which is best and permanent fix to this. You can find your php.ini file as per your OS using below :

  • Linux: /etc/php/{PHP_VERSION}/apache2/php.ini (for PHP running with Apache)
  • Windows: C:\php\php.ini or C:\Windows\php.ini
  • macOS: /private/etc/php.ini

Open the php.ini file and find below values and update as per your need for size:

upload_max_filesize = 20M
post_max_size = 100M

  • Save the changes and close the php.ini file.
  • After making changes to the php.ini file, you may need to restart your web server (Apache, Nginx, etc.) for the new settings to take effect.

On shared hosting you may not have access to this file so you might need to contact your hosting provider.

2. Using .htaccess file (Only for apache server ):

Open your projects .htaccess file (on root) and add below configuration:

php_value upload_max_filesize 20M
php_value post_max_size 100M

So, by this configuration any .php files that resides in that directory will take this configuration and allow to proceed.

Please note that this method will work only if the server has the AllowOverride directive set to allow the use of .htaccess files.

3. Using inline PHP functions:

You can set the limit directly in your code via below php functions :

ini_set('post_max_size', '100M');
ini_set('upload_max_filesize', '20M');

This works only till php versions 5.3. For later versions this settings are in PHP_INI_PERDIR directives so they can’t be set using ini_set. For that use .user.ini function from next method.

4. Using .user.ini :

Create a file called .user.ini in the directory where the PHP file scripts reside and add below content :

upload_max_filesize = 20M
post_max_size = 100M

This solution will work for later versions of PHP as well. Also in some frameworks requires this file placed in the same directory as our projects index.php.

So that’s it on how to update upload_max_filesize and post_max_size in PHP with examples. Hope this finds you hepful.

See other articles on PHP here.