Max file size in php ini

How to change the maximum upload file size for PHP scripts

This article describes how to change the maximum upload file size for PHP scripts by using the upload_max_filesize and post_max_size php directives. 

Changing the maximum upload file size

By default, the maximum upload file size for PHP scripts is set to 128 megabytes. However, you may want to change these limits. For example, you can set a lower limit to prevent users from uploading large files to your site. To do this, change the upload_max_filesize and post_max_size directives.

To ensure that file uploads work correctly, the post_max_size directive should be a little larger than the upload_max_filesize. For example, the following settings demonstrate how to set a file upload limit to 20 megabytes:

upload_max_filesize = 20M
post_max_size = 21M

To verify the current value of the upload_max_filesize directive and other directives, you can use the phpinfo() function. For more information, please see this article.

Setting the directives

The preferred way to set php directives is using the graphical tools in the control panel.

For accounts using the cPanel control panel, see this article.

For accounts using the Plesk control panel, see this article.

Many accounts also allow the use of a custom php.ini file.  For instructions to create a custom php.ini file, see this article.

More Information

  • To view a complete list of php.ini directives, please visit http://www.php.net/manual/en/ini.list.php.
  • For more information about the upload_max_filesize directive, please visit http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize.
  • For more information about the post_max_size directive, please visit http://www.php.net/manual/en/ini.core.php#ini.post-max-size.

Note 1: The file php.ini is not part of Drupal. It is a part of the PHP configuration. To locate it, look up your PHP configuration. If you're on shared hosting, you may not have access to it.

Note 2: Increasing PHP upload size is different from increasing PHP memory limit. You can learn how to increase memory limit here.

Drupal's limits on upload file size are determined by your server's PHP settings (as well as Drupal 6's specified settings that can be set at Admin > Site Configuration > File Upload). The default values for PHP will restrict you to a maximum 2 MB upload file size.

On the settings page for the upload module, Drupal calculates and displays the maximum file size that you can set based upon two PHP settings: 'post_max_size' and 'upload_max_filesize'. Since 'post_max_size' is the limit for all the content of your post, many people choose 'post_max_size' to be a multiple of 'upload_max_filesize' to allow multiple files to be uploaded, but this is not essential. The upload module limits the size of a single attachment to be less than either post_max_size, or upload_max_filesize, whichever is smaller. The default PHP values are 2 MB for upload_max_filesize, and 8 MB for post_max_size.

Depending on your host, changing these two PHP variables can be done in a number of places with the most likely being php.ini or .htaccess (depending on your hosting situation).

For example, to increase the limit on uploaded files to 10 MB:

  • Add the below to the relevant php.ini file (recommended, if you have access). Note that for some hosts this is a system-wide setting. However, for hosts running PHP as a CGI script with suexec (for example) you may be able to put these directives in a php.ini file in your Drupal root directory.
    • upload_max_filesize = 10M
    • post_max_size = 10M
  • Add the below to your .htaccess file in your Drupal root directory.
    • php_value upload_max_filesize 10M
    • php_value post_max_size 10M

The PHP documentation states that the memory_limit setting also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. If this is an issue, see the page on how to Increase memory available to PHP (3 methods)

Drupal also allows/enforces its own size limits, independently of what PHP allows. These are found in Drupal 6 at "Administer » Site configuration » File uploads" ( /admin/settings/uploads ). In Drupal 7 size limit is controlled on per-field basis. The Drupal settings cannot be larger than those permitted by PHP, but may be smaller if you haven't updated them, so remember to check there also, after updating the php.ini.

First Option: Use this option in case you have permission to edit the php.ini file.

The upload_max_filesize and the post_max_size settings in the php.ini need to changed to support larger files than the php default allowed size.

set upload_max_filesize to the maximum size of file which need to be uploaded.

post_max_size should be set to greater than upload_max_filesize to handle the file and the size of form post variables part of file upload form. In case multiple files are uploaded in the file upload form at a time, the post_max_size should be set to (upload_max_filesize * no of files)+ size of post variables.

upload_max_filesize=30M
post_max_size=31M

No need to update memory_limit and max_execution_time php settings, in case file is just moved using the move_uploaded_file to the final path in file upload php script. But if the file upload script is processing the file or reading the file to a variable or doing any other processing which use memory and time etc, memory_limit and max_execution_time should be set.

Set memory_limit to amount of memory needed for the php script to run and max_execution_time to the time in seconds required to run the script.

memory_limit = 100M
max_execution_time = 120

Restart the webserver after the php.ini is changed for it to take effect.

Second Option: Use this option in case you do not have permission to update the global php.ini settings or to improve security.

Copy the upload php scripts to a new Child folder say "Upload" and create a file ".user.ini" in the new folder. Also make sure to update the file upload form action attribute to post to the new Script under the "Upload" Folder.

Add below settings to the newly created file. ".user.ini".

upload_max_filesize=30M
post_max_size=31M

;below are optional
memory_limit = 100M
max_execution_time = 120

"user_ini.filename" php.ini setting can updated to give the user setting file another name.

This option improves the security as the upload_max_filesize, post_max_size,memory_limit, max_execution_time are changed only for the scripts in the new folder and will not impact php settings for scripts in other folders and prevents any unwanted resource utilization by bad scripts.

Please refer below links for more information on ".user.ini" settings

https://www.php.net/manual/en/configuration.changes.modes.php

https://www.php.net/manual/en/ini.list.php

https://www.php.net/manual/en/configuration.file.per-user.php

Restart the webserver for the new .user.ini changes to take effect.

Note:

The memory_limit and max_execution_time setting can also be set in the php upload script using ini_set and set_time_limit function instead of updating php.ini file .

If this option is used, not need to update the memory_limit and max_execution_time setting in the ini file.

Add below to the top of the php file upload script

ini_set('memory_limit', '100M');
set_time_limit(120);

What is the maximum upload file size in PHP?

To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable's in your php. ini file. In addition, you can also set the maximum number of files allowed to be uploaded simultaneously, in a single request, using the max_file_uploads .

How can I upload large files over 500MB in PHP?

How to upload large files above 500MB in PHP?.
By changing the upload_max_filesize limit in the php. ini file..
By implementing file chunk upload, that splits the upload into smaller pieces an assembling these pieces when the upload is completed..

How can I upload more than 2 MB in PHP?

by default PHP will not handle file uploads larger than 2MB, if one requires PHP to handle larger files then one must set upload_max_filesize and post_max_size in your php. ini file to be larger than 2MB.

How do I change max upload file size in PHP?

How to Increase File Upload Size in PHP.
Open the php. ini file in the text editor..
Search for upload_max_filesize variable and specify the size which you want to increase. upload_max_filesize = 128M..
Search for post_max_size variable and specify the size which you want to increase. ( ... .
Once done, save the modified php..