How to change the maximum upload file size in PHP

  •  
  •  
  •  
  •  
  •  
  •  

Hi friends, in this tutorial, I will help you to solve the upload_max_filesize and post_max_size issue. There are many ways to solve the upload file size problem. We will discuss these options one by one to increase file upload size. Let’s see how to change the maximum upload file size in PHP.

The file upload feature is very common and important functionality for web applications. Files can be excel, word, csv, pdf, jpg and so on. Similarly, file size can vary in its size, it may be less than 2MB or greater than 2 MB. Whatever, all we know that default file size in PHP is 2MB to upload into the server. If you try to upload a file which is more than 2MB then you will get a fatal error like “The uploaded file exceeds the upload_max_filesize“.

How to change the maximum upload file size in PHP

Change The Maximum Upload File Size in PHP

PHP providing many options to increase or decrease upload file size limit. Upload file size limit can be changed through .htaccess file, php_init() method or php.ini file.

Change The Maximum Upload File Size in .htaccess

You can set a lower limit to prevent users from uploading large files to your website. And to achieve this change the upload_max_filesize and post_max_size directives in a .htaccess file. Open .htaccess file using a text editor and add the following line to it.

php_value upload_max_filesize 20M

In place of 20M you can set as your requirement.

php_value post_max_size 21M

Similarly, change the 22M as per your requirement and save the .htaccess file.

.htaccess file – PHP 5.x

<IfModule mod_php5.c>
   php_value upload_max_filesize 20M
   php_value post_max_size 22M
</IfModule>

.htaccess file – PHP 7.x

<IfModule mod_php7.c>
   php_value upload_max_filesize 20M
   php_value post_max_size 22M
</IfModule>

Change The Maximum Upload File Size in php.ini file

Open the php.ini file using a text editor and find the below lines one after one –

upload_max_filesize=2M
post_max_size=8M

Now change the values according to your requirement. For example, see the below code –

upload_max_filesize=20M
post_max_size=22M

There is one more option in the php.ini file which you can modify. That is –

max_file_uploads = 20

The max_file_uploads controls the number of files uploads at once during form submit.

Change The Maximum Upload File Size using PHP ini_set() function

PHP has init_set() function by which you can set the upload_max_filesize and post_max_size value to increase or decrease the upload file size. But, remember they work only with PHP version less than 5.3.

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

About Mitrajit

One thought on “How to change the maximum upload file size in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove you are a human *