PHP How to Upload Large Files

There is always a need to upload large files in our application, With PHP how to upload large files is the main topic here, Initially, PHP have default 2MB file limit for upload, So we have to change that setting to allow application to upload larger files than 2MB.
PHP How to Upload Large Files
How we change this, there are 3 ways to do this,

  • Using php.ini
  • Using .htaccess file
  • Using ini_set() function

Now, Following parameters need to set to allow larger file upload,

PHP Directive Brief Description Example
upload_max_filesize
  • It specifies the maximum file size (in bytes) that the PHP engine will accept.
  • The default value is 2M (2 * 1048576 bytes).
upload_max_filesize = 4096
post_max_size
  • It specifies the maximum size (in bytes) of HTTP POST data that is permitted.
  • The default value is 8M (8 * 1048576 bytes).
  • Make sure this value is greater than that of the upload_max_filesize directive.
post_max_size = 10M
memory_limit
  • It specifies the maximum amount of memory (in bytes) that is allowed for use by a PHP script.
  • The default value is 16M (16 * 1048576 bytes).
  • This value should be greater than that of the post_max_size directive.
memory_limit = 20M
max_input_time
  • It specifies the maximum amount of time (in seconds) that is allowed for each PHP script to receive the client’s HTTP request.
  • The default value is 60.
  • If you need to support large file upload, you may need to increase this value to prevent timeouts.
  • Note that some users may have a slow connection. You have to take that into account.
max_input_time = 90
max_execution_time
  • It specifies the maximum amount of time (in seconds) that is allowed for each PHP script to execute.
  • The default value is 30.
  • If you need to process large uploaded files with PHP, you may need to increase this value to prevent timeouts.
max_execution_time = 60

You might like:

Leave a Reply