How to Upload Large Files in PHP

Share this article

Uploading a file from a web form in PHP is easy. The online manual provides a Handling File Uploads
section, and there are several articles on sitepoint.com, including How To Handle File Uploads With PHP by Kevin Yank. One of the most popular uses is image uploads. Your users can submit photographs from a form without resorting to FTP or other convoluted methods. HTML5 and Flash also permit drag and drop, so the operation is likely to become easier as browsers evolve. This is where the problems can begin. Camera manufacturers continually brag that they have a larger set of megapixels than their competitors. It’s all rubbish, of course — unless you’re a professional photographer or need to print extremely large images, anything over 4MP is fairly pointless and lens quality is much more important. However, even low-end compacts have 12MP and mobile phones have more than 5MP. The result is that a typical snapshot can easily be 6MB in size. By default, PHP permits a maximum file upload of 2MB. You can ask users to resize their images before uploading but let’s face it: they won’t. Fortunately, we can increase the limit when necessary. Two PHP configuration options control the maximum upload size: upload_max_filesize and post_max_size. Both can be set to, say, “10M” for 10 megabyte file sizes. However, you also need to consider the time it takes to complete an upload. PHP scripts normally time-out after 30 seconds, but a 10MB file would take at least 3 minutes to upload on a healthy broadband connection (remember that upload speeds are typically five times slower than download speeds). In addition, manipulating or saving an uploaded image may also cause script time-outs. We therefore need to set PHP’s max_input_time and max_execution_time
to something like 300 (5 minutes specified in seconds). These options can be set in your server’s php.ini configuration file so that they apply to all your applications. Alternatively, if you’re using Apache, you can configure the settings in your application’s .htaccess file:

php_value upload_max_filesize 10M
php_value post_max_size 10M
php_value max_input_time 300
php_value max_execution_time 300
Finally, you can define the constraints within your PHP application:
ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);
PHP also provides a set_time_limit()
function so you don’t need to set max_execution_time directly. Setting the options in your PHP code is possibly more practical, since you can extend the execution time and increase the file size when your application is expecting a large upload. Other forms would revert to the default 30-second time-out and 2MB limit. Do you have any other tips for uploading large files in PHP?

Frequently Asked Questions (FAQs) about Uploading Large Files in PHP

How Can I Increase the Maximum File Upload Size in PHP?

The maximum file upload size in PHP can be increased by modifying the ‘upload_max_filesize’ and ‘post_max_size’ directives in the php.ini file. The ‘upload_max_filesize’ directive defines the maximum size for uploaded files, while the ‘post_max_size’ directive defines the maximum size for POST data allowed. It’s important to note that ‘post_max_size’ should be larger than ‘upload_max_filesize’ to accommodate additional data like form fields. After modifying these values, save the php.ini file and restart your web server for the changes to take effect.

What is the Default Maximum File Upload Size in PHP?

By default, the maximum file upload size in PHP is set to 2MB. This is defined by the ‘upload_max_filesize’ directive in the php.ini file. However, this size may not be sufficient for all applications, especially those that require the upload of large files. In such cases, the maximum file upload size can be increased by modifying the ‘upload_max_filesize’ and ‘post_max_size’ directives in the php.ini file.

Why am I Unable to Upload Large Files in PHP?

There could be several reasons why you’re unable to upload large files in PHP. The most common reason is that the file size exceeds the maximum file upload size defined by the ‘upload_max_filesize’ directive in the php.ini file. Other possible reasons include insufficient memory or disk space, incorrect file permissions, or server configuration issues. It’s important to check these factors if you’re having trouble uploading large files.

How Can I Check the Maximum File Upload Size in PHP?

You can check the maximum file upload size in PHP by using the ‘ini_get’ function to retrieve the value of the ‘upload_max_filesize’ directive. This function returns the current value of the specified configuration option. Here’s an example:

echo ini_get('upload_max_filesize');

This will display the maximum file upload size in bytes.

Can I Change the Maximum File Upload Size at Runtime?

Yes, you can change the maximum file upload size at runtime using the ‘ini_set’ function. However, this change will only affect the current script execution and will not persist after the script finishes executing. Here’s an example:

ini_set('upload_max_filesize', '10M');

This will set the maximum file upload size to 10MB for the current script execution.

What Happens if a File Exceeds the Maximum Upload Size?

If a file exceeds the maximum upload size, PHP will automatically reject the file and generate an error. The $_FILES array will contain an error code indicating the reason for the rejection. You can check this error code to determine whether the file was rejected due to its size.

How Can I Handle File Upload Errors in PHP?

File upload errors in PHP can be handled by checking the ‘error’ element of the $_FILES array. This element contains an error code that indicates the status of the file upload. You can use a switch statement to handle different error codes and display appropriate error messages.

Can I Upload Multiple Files at Once in PHP?

Yes, PHP supports multiple file uploads. You can use the ‘multiple’ attribute in the file input field to allow users to select multiple files. The $_FILES array will contain an array for each uploaded file, which includes information like the file name, type, size, and temporary location.

How Can I Secure File Uploads in PHP?

Securing file uploads in PHP involves several steps. First, validate the file type and size to prevent the upload of malicious files. Second, use the ‘move_uploaded_file’ function to move the uploaded file to a secure location. This function ensures that the file was uploaded through a POST request, which helps prevent file injection attacks. Finally, set appropriate file permissions to restrict unauthorized access.

How Can I Monitor the Progress of File Uploads in PHP?

Monitoring the progress of file uploads in PHP can be achieved using the PHP Session Upload Progress feature. This feature provides information about the progress of file uploads, including the total size of the files, the amount of data uploaded so far, and the estimated time remaining. You can use this information to display a progress bar or other visual indicator to the user.

Craig BucklerCraig Buckler
View Author

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.

configurationPHP
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form