Uploading large(big) files in PHP using .htaccess

I’ve seen that many of my friends are struggling with the uploads of the bigger or larger files in PHP. After looking at their struggle, i’m here to solve the problem of uploading larger or bigger files in PHP.

Most of the web servers are configured such a way that a user can only upload the maximum file size of 2MB. So there might be the problem for the people who wants to upload the .pdf file of size around 15MB. But, you can increse the maximum upload file size limit by using .htaccess file.

Here is a small tips for you which you can use to upload such a large file using file field of the form and move_uploaded_file() function in PHP.

1) Create a .htaccess file in the root folder of web server.

2) Put the following code in side the .htaccess file and save it.

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

Now you can upload the file-size up-to 20MB in a simple way using file field in your html form and move_uploaded_file() function available in PHP. In the above .htaccess file, uploading capability is increased by the four parameter first one is maximum file size for uploading, second one is maximum size of the post data , third one is maximum time in seconds a script is allowed to run before it is terminated by the parser and last one is maximum time in seconds a script is allowed to parse input data such as like file uploads, POST and GET data.

You can change the above parameter to upload the bigger file size than 20MB.

77 thoughts on “Uploading large(big) files in PHP using .htaccess

  1. will

    I would strongly recommend against using the .htaccess method to achieve this.

    Rather, in whichever script you intend to upload a potentially large file from, use ini_set(). As soon as the script exits or the file upload process is finished, use ini_restore().

    In this manner, users which may have access to other upload scripts won’t be able to waste your server’s processing power, bandwidth or disk space if they aren’t supposed to!

    In retrospect however, if this setup is used in an admin-only area, then it would be a quick and dirty solution.

  2. Thanks for the comment..i like your approach for using ini_set() and ini_restore(), its the best approach but you lack some knowledge regarding these variables look at this link properly, http://www.php.net/manual/en/ini.php#ini.list

    upload_max_filesize is changeble in PHP_INI_PERDIR only which means that “Entry can be set in php.ini, .htaccess or httpd.conf”.

  3. will

    Maybe my description was not very clear.

    The upload_max_filesize (and some other) ini variables may be set in any of the three ways you have described.

    (In my experience) I would avoid the use of the .htaccess method becuase all scripts within the directory (and deeper) where the upload script is called will see the same ini setting.

    With that said, it would be very easy for someone to create a denial-of-service attack by overloading pages which collect form data with junk.

    I also forgot to mention that ini_restore is automatically called by php when a script exits if it has not already been done.

  4. Hey, I am really interested in getting PHP to upload larger files.

    So which is better? I do not own the servers I use, I use a “server service”, and I think a lot of web developers do, but how do I get PHP to increase the size of file upload temporarily? I am assuming I need to put it into the form section, which is where the upload occurs, and what arguements do I use for the “ini_set()” function?

    Any help on this would be great! Tell me success stories.

  5. hi jcapinc….ini_set() function is no longer useful when uploading the larger files in php so better to use .htaccess approach.

  6. Thanks for the PHP tips.
    Bigger and larger mean exactly the same. No person who speaks English fluently in any country anywhere would say “bigger or larger”.

  7. jack

    Nice trick here, is tht possible i can set the php_value max_execution_time and php_value max_input_time to 0 for unlimited time in .htaccess?

  8. Good tip but a little incomplete. You’re assuming that the server is configured to honor what you override via .htaccesss, Apache’s Allow Options has to be set so you can do so.

    You’ll also want post_max_size to be a little bigger than upload_max_filesize if you have other fields in the form. You’ll want it a lot bigger if you have multiple file upload fields in one form.

    Finally, once you get all of that sorted, you might still run into Apache’s LimitRequestBody directive if its been set in your server config. This limits the max post size Apache will allow from any POST (not just PHP).

  9. I think the following step is simple one that can able to fix this Issue:

    1. Goto php.ini file,inside php folder.
    2. search for variables ‘upload_max_filesize’ & ‘post_max_size or max_post_size’
    3. change its default value from 2M to 10M(or depends on ur need ).
    4. save the php.ini file.
    5. restart the server

    congrates you are done with it.

  10. hi anandsharma you can do that in localhost but how can you edit it in the server? i think in server you’ve to use .htaccess anyway.

    And i really appreciate the comment by Oscar M., ya i assumed that Apache’s Allow Options is set in the server.

    And jack you’ve to set them to maximum values not to 0…..

  11. Jahuf

    @Roshan:

    Passing 0 to max_execution_time implies unlimited script execution time.

  12. Thanks Jahuf for pointing out my fault, sorry anandsharma Jahuf is right….

  13. Johnson

    It doesnt really solve the problem. There are still limitations of http post

    What if i wish to upload 100MB video?

    How does Youtube achieve it? How does yousendit achieve it/

  14. Johnson, youtube and yousendit have their own dedicated server and they can configure these parameter as their need without any heck but as for the general user they to buy some space from web hosting company and they’ve limitations in those account and for this technique to work properly Apache’s Allow Options is to be set in the server.

  15. vinay

    hi everyone,

    i tried changing the php.ini file and changed these all 3 parameters

    upload_max_filesize
    post_max_size
    max_execution_time

    according to my need still I am not ableto upload big files, not even files of size 1+ mb. it is only allowing very small files to be uploaded like 100kb. actually my sql query is failing where I am inserting the file data and content in to the table.

    any help??

    regards
    vinay

  16. try phpinfo() you can get lots of info regarding various modules loaded and upload limit of your server. And furthermore check your query again..

  17. the-ir

    after modified your php.ini file you have to restart your apache server :

    c:> net stop apache
    c:> net start apache

    i have tried and it’s done.

    now i have a question, how to modify php.ini at hosting server? .htaccess not effected at awardspace.com

    any help? thanks.

  18. .htaccess doesnot do any effect in awardspace.com’s free hosted account.The server should allow the overriding.

  19. Sheraz

    Great, It helped me so far.And i liked the approach too.

  20. Thanks sheraz…….

  21. shaan

    is it possible to upload 500mb file using .htaccess process in server?

  22. shaan

    i hv used .htaccess process,after creationg a .htaccess file in root directory of web server i enter the following code in the .htaccess file:

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

    when i tried to open the page,the following error message is shown in the browser:

    Internal Server Error
    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

  23. does your server support over-riding? you server must be configured to AllowOverride All in the configuration file of apache..check this once

  24. Mohamed Moupasher

    WOW very useful information despite I didn’t reach that level in my study of PHP, but anyway these information will be useful for me in the future.

    Many Thanx Roshan

  25. I ahve max_execution_time and max input time set to 6 hrs, my max post and max file in php.ini is 850mb

    tha max I am able to upload and for my php page to continue is about 50MB if I go larger, the file uploads though the infamous download dialog appears for the page I am on as if php timed out. I wish I new a fix for this.. any one know one or why this download box comes up instead of my page finishing.. http://www.filitv.com/info.php for php info.

  26. when I tried to look at the phpinfo() file i see this

    Forbidden

    You don’t have permission to access /info.php on this server.

  27. I could not for the life of me get php to work with large post data over about 80MB

    I found this
    http://www.webdice.org/uber_uploader/

    Its AWESOME and works.. as for the forbidden, That ALL the sudden just came up last night when I was testing.. right about when you tried it.. I do not know why or how to fix it.. I did not change anything. I am on a dedicated server and I am the administrator.. If you are able to provide any help please do..

  28. I exactly couldn’t understand what to fix ? And furthermore , you can change each and every setting of php.ini in your dedicated server.

  29. Hi,

    i want to upload 200M mp3 file. i used .htaccess file. it works well in my localhost. but it is not work in online. In online, only it allows 28.6M. I don’t know the actual problem. htaccess file has the following content

    AddHandler application/x-httpd-php .php
    php_value post_max_size 200M
    php_value upload_max_filesize 200M
    php_value memory_limit 300M
    php_value output_buffering on
    php_value max_execution_time 259200
    php_value max_input_time 259200
    php_value session.cookie_lifetime 0
    php_value session.gc_maxlifetime 259200
    php_value default_socket_timeout 259200

    Help me plz. plz send the actual problem and it’s solution.

    Thank U.

  30. What is the error you may getting ? You may write the hosting provider regarding the problem of the issue, they may also help you..

  31. Hi,

    Thanks 4 ur caring. Server name is servage.net. The following error is occured.

    “the Connection was Reset”

    I asked this problem to that server. but they simply asked change ur .htaccess file like
    php_value upload_max_filesize 200Mb

    already the code has this value. they did’t understand it. what is the problem? I totally confused. help me plz.

    Thank U.

  32. Worked like a charm for me, after scratching my head when I’d just increased the “php_value upload_max_filesize” and my client’s 10Mb file still wasn’t uploading to his site.

    Thanks for the post Roshan.

  33. Yea, people need to understand that if your hosting from someone elses server, your less likely to be able to change these values.

    The Internal server error, or page timeout means the hardware or actual OS of the server has been configured to allow a maximum transfer in one session.

    This is something you would prorbably want to contact your hosting provider and enquire about. Depending on the type of hosting or the way they setup their server, they may be able to increase this for you.

    Thanks for yet another useful tut, cheers rosh.

  34. nagaraj.r

    how to find out file size in php using ajax…?
    plz help me…!

  35. Message for Shaan

    As far as php.ini files are concerned, you will need to use the php.ini files in each of the indvidual folders that require the adjusted settings. This is because the server is running as PHP 5 with suphp enabled. This causes all php_anything lines in .htaccess files to disable your site.

    Contact you administrator to check the php setting and suphp enabled.

    This should resolve your issue.

    Dan

  36. Many many thanks :)

  37. Kevin

    I am trying to exactly upload PDF files from Acrobat Reader and I get Time out errors.

    I think you .htaccess code will solve this problem easily, but when I put your lines of code into my .htaccess file, I get – 500 Internal Server Error screen

    I think I just don’t understand the formating or something of putting those 4 lines into my file. If I put even 1 into my file it give me this error.

  38. Paul

    The solution given here WILL NOT WORK for those running PHP5. The hosting company I use (HostMonster) has PHP 5.2.6 installed and I cannot change upload_max_filesize using ini_set or .htaccess. The only way for me to change upload_max_filesize is to install the php.ini master file, put it in the directory where I want the change to affect and modify the upload_max_filesize from that file.

  39. Disha

    Hi ! Roshan !

    i already access .htaccess file but it doesn’t work !
    actually in my project i want to upoload a show-reel in mpg format n client needed 107 mb file to upload !

    right now at testing i have uploaded 12.5 mb video but not succeed…error is not there but nothing is there n it just sopped !

    plz reply soon……….it’s urgent !

  40. Disha

    plz…..urgent !!!

  41. Alper

    hi all,
    please also check this;

    http://www.modwest.com/help/kb5-219.html

    if your PHP handler is CGI, you must use php.ini.
    It depends on your server’s configuration.

    here is I quoted it;

    “If you have php error logging with reporting set to E_ALL then you will see the PHP Error reported:

    PHP Warning: POST Content-Length of 9470478 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

    The problem is unset or incorrect settings in your /etc/php.ini file.

    To allow file uploads larger than the default 8 megabytes, put these settings in your /etc/php.ini:

    post_max_size = 16M
    upload_max_filesize = 16M

    You can check phpinfo() output to make sure that those values get set correctly.

    Also make sure that your script does not have an html form input tag for MAX_FILE_SIZE that is smaller (in bytes) than the size of the files that you are trying to upload.

    Now you will be able to upload files up to 16 megabytes.

    NOTE: although PHP.net lists post_max_size as being a PHP_INI_PERDIR, because of the way we run PHP in CGI, this cannot be changed in your .htaccess file. However, there is a workaround:

    Copy your current php.ini file into the directory that you require the custom post_max_size in.

    **THE NEXT STEP IS VERY IMPORTANT**
    Obviously, we don’t want the outside world to have access to your settings in your php.ini file. The solution? Drop an .htaccess file in the same directory with the following lines in it:

    Deny From All

    This will deny any web requests to any file in that directory ending in .ini; thus preventing your PHP settings from being seen by others.

    Now you have a secure php.ini file that you can modify settings in to affect the .php files within that directory.”

  42. dara

    it work

    good your tutorial.

  43. Mayuresh

    Hi Roshan,
    Nice for collecting all the information in your site. Will you please guide me for uploading large video file 1 GB over the server. I am having php 5.2.6 ver and its under CGI.
    I have set max_upload_limit to 1000M, Increased the file execution time ,increased post limit to 512k in php.ini. but some time file get upload of 0 kb or i get error page cannot found .
    I am using uber-upload cgi scrip.
    Is there any other way to upload large file in chunk if internet connection restart upload from where it has been left .

    Please let me know your valuable feedback.

    Thanks and Regards
    Mayuresh Band

  44. @ Mayuresh
    Your server hosts would have a master limit set, its best to contact them to ask. This limit is for HTTP transfers only, for a 1gb file you are best to go with FTP.

    HTTP is likely to time out after a certain amount of megabytes anyway. I’m sure there are ways around that but they would require more hard coding. If you want your users to be able to upload these 1gb files, i recommend setting up a guest FTP account.

  45. paladone

    .htaccess it can be running on linux, everyone who know how to cofig php to support the larg file uploading, please advice

  46. tamilarasan

    it works well…
    your tutorial is great…..

  47. I have put .htaccess file in root folder as you said above. The changes are reflect at phpinfo(). But still I am unable to upload more than 3 Mb images. They show server (IO) error when uploading. I am using swfuploader. It works very good with file smaller than or equal to 2 mb.

    Can’t we upload simply using move_uploaded_file() function… I am pissed off with this problem.

    Can you suggest me with some solution…

  48. Hiey All,

    above comments is great.
    can some one guide me to upload larger file without using .htaccess file.
    only used php code. concept used as chunk of file.

    Thanks
    Priyank Sharma
    (+91-9928566520)

  49. Raees Khan ShahSani

    Well done, it works… Shukrya!!!

  50. apple

    Hi, if I created .htaccess, do I need to call it from my codes or just upload it. And where can I upload that? In my public_html or inside the folder where my files resides?

    Thanks.

Comments are closed.