[TIPS] – Upload BIG (very large) file in PHP using HTACCESS


 

Logo of the Apache Software Foundation
Image via Wikipedia

 

This blog should have been the first one, but somehow slipped through🙂. In my early days of coding I found a nice tip to upload a large (really big) files to server with the help of .HTACCESS.

By default, PHP.INI configuration is configured to upload a max size of 2MB (security concern?), I know you can say it just mater of modifying the value in PHP.INI file and restart the server. Yes I agree, but you can do this when you own the server machine, think of a shared hosting server.

Most of the users in the world uses shared hosting compared to VPS/Dedicated server. Hence it becomes difficult to request the support to lift the limit of max-upload-size. But don’t be panic, there is a neat/simple solution this problem, APACHE makes our life easier to allow the PHP.INI value dynamically with the help of .HTACCESS. Yes, APACHE will read the configuration value from .HTACCESS file and over-write the value present in PHP.INI file.

Add the following lines to your .htaccess file (located in your DOCUMENT_ROOT) and now try to upload a file of size 20MB:

php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200

You might wonder why can’t we stop with upload_max_filesize? Nope, you can’t since file uploading is a part of POST method and we have to make sure:

  1. post_max_size is >= upload_max_filesize
  2. max_execution_time >= time it takes to upload upload_max_filesize file (depends on the server speed)
  3. max_input_time >= max_execution_time

I hope this helps for some.

Happy Coding!

[TIPS] – Upload BIG (very large) file in PHP using HTACCESS

2 thoughts on “[TIPS] – Upload BIG (very large) file in PHP using HTACCESS

  1. Hi there,

    I need to upload a very large file (jpg , zip doc or xml).It is in GB.
    What should I do,I don’t think so that the above will work fine in this case.

    Please suggest!!

    1. Wow, the above suggestion would still work with the appropriate value set for those variables plus with a library which would send the data in a parallel chunks.

      Please visit this plugin: http://www.plupload.com/documentation.php and use it on your project. This plugin has an option to send the file(s) into various chunks.

      Note:
      Its a huge file, make sure you have enough of bandwidth and a powerful hardware to support the performance of your website.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s