Upload Max Filesize – Large File Uploads in PHP

by Hiroshi on September 9, 2008

in File Upload, Tricks

By default there is a restriction of 2 Megabytes file upload at a time for PHP. If we have to upload media files or other large files then we will have to change some settings in php.ini.

  • Locate the php.ini file in the folder your upload script resides in. If none exists, install a new php.ini file from the PHP config icon in cPanel. Then, use the File Manager to rename php.ini.default to php.ini
  • In the php.ini file, locate the line called ‘upload_max_filesize = 2M’
  • Modify that entry to read ‘upload_max_filesize = 10M’, or whatever your script requires. (M = megabytes)

PHP presents a very versatile and user friendly interface for handling file uploads. The default installation is not capable of working with files greater than 2 Mega Bytes size. Here are some more options for configuring PHP to handle large file transfers.

php.ini settings

The php.ini file contains all the configuration settings for PHP. These can be overridden by directives in apache .htaccess files or even with in the scripts themselves but for the moment let’s just concentrate on the ini file.

This file contains the following settings that we need to modify according to our needs.

  • file_uploads
  • upload_max_filesize
  • max_input_time
  • memory_limit
  • max_execution_time
  • post_max_size

file_uploads

If you set this off, uploading is disabled.

upload_max_filesize and post_max_size

Files are POSTed to the webserver in a format known as ‘multipart/form-data’. The post_max_size sets the upper limit on the amount of data that a script can accept in this manner. Ideally this value should be larger than the value that you set for upload_max_filesize.

It’s important to realize that upload_max_filesize is the sum of the sizes of all the files that you are uploading. post_max_size is the upload_max_filesize plus the sum of the lengths of all the other fields in the form plus any mime headers that the encoder might include.

According to the PHP documentation you can set a MAX_UPLOAD_LIMIT in your HTML form in a hidden field to suggest a limit to the browser but this is not 100% reliable way.

<form>
<input name="MAX_FILE_SIZE" type="hidden" value="10737418240" />
</form>

memory_limit

When the PHP engine is handling an incoming POST it needs to keep some of the incoming data in memory. Setting too high a value can be very dangerous because if several uploads are being handled concurrently all available memory will be used up and other unrelated scripts that consume a lot of memory might effect the whole server as well.

max_execution_time and max_input_time

These settings define the maximum life time of the script and the time that the script should spend in accepting input. If several mega bytes of data are being transfered max_input_time should be reasonably high. You can override the setting in the ini file for max_input_time by calling the set_time_limit() function in your scripts.

While working with uploading in PHP, consider above settings and options in mind and use these very carefully.

Related Posts

Previous post:

Next post: