00 Votes

PHP: Upload of large Files

Tutorial by Stefan Trost | 2015-06-18 at 21:09

By default, PHP is often only allowing file uploads having a maximum file size of 2 MB in its default settings. Usually, this limit is often already reached when trying to upload some high resolution pictures (I do not want to speak about videos, music or ZIP archives).

Today, I would like to show you how to change this restriction and how to allow larger file uploads. The first possibility is working globally by using the settings in php.ini, the second possibility is working locally via HTACCESS so that you can also define individual values for specific folders.

Please also take into account the limitations of your hoster or provider. In most cases, you are not allowed to change the php.ini if you do not have an own root server.

Settings in php.ini

Depending on the system, you can find the php.ini for example in the directory "/etc/php5/apache2/php.ini", "/etc/php.ini" or "C:\Program Files\PHP\PHP.ini". Open the file with an arbitrary text editor and just the following values:

upload_max_filesize = 100M
post_max_size = 128M
memory_limit = 256M
max_input_time = 300
max_execution_time = 300

The most important value is "upload_max_filesize". Here, we are setting the value to 100 MB. This sets the limit for the maximum size of uploaded files to 100 MB.

To make it work, it is also necessary to check the other values shown here, because even if "upload_max_filesize" is set to the desired value, the file upload can fail because of too low values here. The value "post_max_size" (maximum size of the entire POST data) should be a little bit higher than "upload_max_filesize", the same applies for "memory_limit" (how many bytes can be allocated by one script).

Additionally, we have to take into account that uploading large files can - depending on the Internet connection - take a long time. Therefore, we should also set the values of "max_input_time" and "max_execution_time" as high as necessary. In the example, we are setting the values to 300 seconds (5 minutes). Finally, the script has to run as long as the upload has finished.

The configuration in php.ini applies to the entire system and therefore, the changes will only be applied after restarting Apache/PHP. So, do not forget to restart after changing the values.

Settings in .htaccess File

In order to set the corresponding values locally or only for specific folders or in case that you do not have access to your php.ini at your provider, you can also set the values via .htaccess file within the corresponding folder or within your root directory.

php_value upload_max_filesize = 100M
php_value post_max_size = 128M
php_value memory_limit = 256M
php_value max_input_time = 300
php_value max_execution_time = 300

Using this method, it is also possible to set different values for different directorys.

Change Settings using PHP

At last, I would like to talk about another possibility with which you can change the values directly within a PHP script.

ini_set('upload_max_filesize', '100M);
ini_set('post_max_size', '128M);
ini_set('memory_limit', '256M);
ini_set('max_input_time', '300);
ini_set('max_execution_time', '300);

However, again, you have to note, that this procedure is not allowed with each hosting provider.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.