How to upload large files in PHP

If you are planning to implement file upload functionality in PHP, the first thing that you need to verify is that whether file upload functionality has been turned on or not in your php.ini configuration file. The setting that you need to look for is “file_uploads”.


; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

If you don’t know where your php configuration file is located, then create the below single line PHP file ‘phpinfo.php’ and place it in your docroot folder. 


<?php

phpinfo();

?>

Now access the file via browser http://somedomain.com/phpinfo.php and look for the “Loaded Configuration File” value.

 

1. Configure PHP

 

By default PHP is configured to allow files upload of size upto 2M.

Now if we have to upload files say upto 500M then how do we do it?

We need to adjust the following PHP configuration directives:

  1. upload_max_filesize: By default this value is 2M. We need to increase it to the maximum size of single file that we want to upload.
  2. post_max_size: It defines the maximum size of POST data that PHP will accept. This value should be greater than ‘upload_max_filesize’.
  3. memory_limit: This sets the amount of memory a PHP script is allowed to use during its execution. Set this to a value greater than ‘post_max_size’ so that PHP script can load and process the uploaded file.

If you notice PHP execution timeout in your log file which may be the case when you are processing the uploaded file like uploading an image, manipulating it and then saving it, then you must consider increasing the following timeout values. The values are to be specified in seconds.

  1. max_input_time: This sets the maximum time in seconds a script is allowed to parse input data, like POST and GET. Timing begins at the moment PHP is invoked at the server and ends when execution begins. This would include populating $_FILES superglobal.
  2. max_execution_time: The time a script is allowed to run after its input has been parsed. This would include any processing of the file itself.

If you are getting memory related error then turn off the output buffering, the PHP configuration directive to be considered is “output_buffering”


output_buffering = Off

 

 

Temporary directory for HTTP uploaded files

 

You might need to consider the your temp file location (uploaded file is initially stored at this location) also. It should have enough space so that it can save the uploaded file. You must keep in mind that multiple users might be uploading files at the same time when deciding the temporary location. 


; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
;upload_tmp_dir =

For these php.ini configuration changes to take effect, you will have restart the apache server if you are using PHP as an apache module. In case you are using PHP-FPM with nginx then you need to restart PHP-FPM.

 

What if you don’t have access to the php.ini file?

 

  • Configure the setting in your application’s .htaccess file
php_value upload_max_filesize 500M
php_value post_max_size 550M
php_value memory_limit 1024M
php_value max_input_time 300
php_value max_execution_time 300

  • Alternatively, you can define the values in your PHP script itself
ini_set('upload_max_filesize', '500M');
ini_set('post_max_size', '550M');
ini_set('memory_limit', '1024M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);

 

2. Configure Webserver

 

Check your web server configuration files for the following setting as they may not allow to upload large size files.

I was using Nginx with PHP- FPM, so for allowing upto 500M uploads I set the ‘client_max_body_size’ as ‘510M’ in my nginx config file (/etc/nginx/nginx.conf).


http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    client_max_body_size 510M;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    .

    .

    .

}

 

To Begin with…

 

  1. I would say, configure the following two php.ini settings:
    1. upload_max_filesize
    2. post_max_size
  2. Test the file upload functionality
  3. Check errors and log files
  4. Reconfigure php and the webserver
  5. Repeat from step 2 until you are successful in uploading the file. 

Leave a Comment

Back to top