Uploading large(big) files in PHP using .htaccess

Advertisement

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.

Popularity: 48% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Force download multiple files in a zip archive using PHP
» Prevent Directory Listing using .htaccess
» Absolute path and Relative path file inclusion in PHP
» Blogger Sucks, WordPress Wowww!!

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

  • will wrote on 15 January, 2008, 18:45

    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.

  • roshanbh wrote on 16 January, 2008, 6:23

    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”.

  • will wrote on 16 January, 2008, 23:14

    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.

  • jcapinc wrote on 20 January, 2008, 4:20

    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.

  • roshanbh wrote on 20 January, 2008, 17:51

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

  • Terry of Astoria wrote on 24 January, 2008, 18:03

    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”.

  • jack wrote on 25 January, 2008, 2:54

    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?

  • Oscar M. wrote on 25 January, 2008, 6:24

    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).

  • anandsharma wrote on 25 January, 2008, 9:16

    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.

  • Roshan wrote on 25 January, 2008, 10:33

    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…..

  • Jahuf wrote on 28 January, 2008, 3:49

    @Roshan:

    Passing 0 to max_execution_time implies unlimited script execution time.

  • Roshan wrote on 28 January, 2008, 6:21

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

  • Johnson wrote on 2 February, 2008, 4:13

    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/

  • Roshan wrote on 2 February, 2008, 17:05

    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.

  • vinay wrote on 2 February, 2008, 21:55

    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

  • Roshan wrote on 4 February, 2008, 6:48

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

  • the-ir wrote on 4 February, 2008, 21:43

    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.

  • Roshan wrote on 5 February, 2008, 5:18

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

  • Sheraz wrote on 11 March, 2008, 7:49

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

  • Roshan wrote on 11 March, 2008, 8:37

    Thanks sheraz…….

  • shaan wrote on 19 March, 2008, 5:32

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

  • shaan wrote on 20 March, 2008, 4:59

    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.

  • Roshan wrote on 20 March, 2008, 5:49

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

  • Mohamed Moupasher wrote on 22 March, 2008, 23:26

    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

  • leeburstroghm wrote on 19 May, 2008, 23:10

    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.

  • Roshan wrote on 21 May, 2008, 5:11

    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.

  • leeburstroghm wrote on 21 May, 2008, 14:53

    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..

  • Roshan wrote on 21 May, 2008, 18:36

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

  • Chitra wrote on 21 June, 2008, 6:17

    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.

  • Roshan wrote on 21 June, 2008, 15:33

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

  • Chitra wrote on 23 June, 2008, 4:54

    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.

  • John Dixon wrote on 17 July, 2008, 9:31

    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.

  • Tarquin wrote on 4 September, 2008, 12:08

    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.

  • nagaraj.r wrote on 16 September, 2008, 7:41

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

  • Dan Adams wrote on 27 September, 2008, 9:12

    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

  • ranacse05 wrote on 29 September, 2008, 23:07

    Many many thanks :)

  • Kevin wrote on 7 October, 2008, 16:36

    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.

  • Paul wrote on 9 October, 2008, 19:46

    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.

  • Disha wrote on 10 October, 2008, 6:29

    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 !

  • Disha wrote on 10 October, 2008, 6:36

    plz…..urgent !!!

  • Alper wrote on 26 October, 2008, 9:14

    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.”

  • dara wrote on 17 February, 2009, 7:30

    it work

    good your tutorial.

  • Mayuresh wrote on 10 March, 2009, 4:39

    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

  • Tarquin wrote on 18 March, 2009, 2:08

    @ 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.

  • paladone wrote on 5 May, 2009, 6:28

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

  • tamilarasan wrote on 7 May, 2009, 6:16

    it works well…
    your tutorial is great…..

  • anup shakya wrote on 4 June, 2009, 23:12

    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…

  • priyank sharma wrote on 5 June, 2009, 11:49

    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)

  • Raees Khan ShahSani wrote on 13 June, 2009, 6:30

    Well done, it works… Shukrya!!!

  • apple wrote on 18 June, 2009, 9:25

    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.

  • apple wrote on 18 June, 2009, 9:57

    Another question, this was regarding localhost. I modify my php.ini already, I increase the upload_max_filesize and post_max_size but then, I cannot upload files more than 2Mb. What do you think guys is the problem? Thanks.

  • bagan wrote on 18 June, 2009, 20:32

    Hello roshan, I created .htaccess file in my web root folder and tested phpinfo page. it was not effected the php settings. actually I don’t have own server. I am using webservices. Is this case.? I am looking for your great help to resolve my problem. my webserver has CGI version php. please help me how we could change upload file size limit. Advanced thanks for you and hope my prob would get resolved soon.

  • balakris wrote on 25 June, 2009, 7:41

    my server is 7 mb file upload size capacity.is there any possible to upload more than 7 mb with help of htaccess(upload_max_filesize and post_max_size)

  • Salman wrote on 2 July, 2009, 7:54

    Debugging tips for people who are unable to get it to work:

    1) Inside the directory that contains the php file upload handler script, create a php file that contains a call to phpinfo(). Open this file in browser and inspect the “local” values (e.g. upload_max_filesize) reported by phpinfo.

    2) Activate additional error logging in php if possible. If you do not have access to php’s global configuration file, you *might* be able to override settings via .htaccess by aadding these lines:

    # enable sending error message to browser
    php_flag display_errors On
    # shows most errors including NOTICEs
    php_value error_reporting 2047

    3) Have a look at apache error log for errors related to apache, not php. Apache error logs are enabled by default and usually stored in a file called ‘error.log’. This gives you further details about configuration errors such as:

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

    4) Try entering numerical values instead of php constants. Example:

    20M => 20971520
    100M => 104857600

  • Web Design Company wrote on 22 July, 2009, 8:16

    WoW. very very thanks. Its working on fine way. Its really a good and easy way.

  • Chien wrote on 4 August, 2009, 12:31

    thanks so much! it works!!!!!!!!!!!

  • not wrote on 19 August, 2009, 7:24

    bla bla bla, i see no deference, it’s not working at all lol

  • Bob wrote on 22 August, 2009, 7:59

    It worked for me but to err on the side of caution (Possible DDOS attacks) I decided to use these same exact settings in the PHP config editor. I tested everything out and it works smoothly now, thank you for sharing this.

  • SlackeR wrote on 5 October, 2009, 3:56

    Can you post a same html or php upload script that I can use even a simple one? I tried this on htaccess and created my php upload script. I gave me errors but when I checked it was able to upload the 34mb file I tried uploading

    A nice script even a simple one that is usable would help. Thanks

  • Kirill wrote on 16 October, 2009, 12:15

    I have trouble uploading realy large files. Up to 5 GB. I have next entries in my .htaccess, but it doesn’t solve the problem.

    LimitRequestBody 0
    php_value upload_max_filesize 10000M
    php_value post_max_size 15000M
    php_value max_execution_time 18000
    php_value max_input_time -1
    php_value memory_limit -1

    I am able to upload 1.5 GB file, but failed with 4 GB.

    What should I check?

  • ganool wrote on 19 November, 2009, 14:05

    i got 500 internal server erro..
    help please

  • Nikhil wrote on 9 December, 2009, 8:30

    @Roshan

    hi roshan i’m exactly the same issue. Even after settings the .htaccess file i cannot upload a 17 mb file my phpinfo() says that upload limit is 50 mb and post limit is also 50 mb and the memory limit is 128mb.

    if i use a .htaccess file the script executes for some time and returns a “500 Internal server”
    with out .htaccess file the sever returns “Connection was reset”.

    Is it some thing to do with the exectuion time of the script ? I tried to extend the script execution time thorugh the .htacess[php_value max_execution_time 900].Stil i’m getting
    “500 Internal server” error.
    Please hep i’m new to php coding………

    Thanks

  • ebusinessuk wrote on 30 December, 2009, 14:42

    It was quite a hurdle to upload large files. In our line of business, files are always macro, never micro. Your tutorial has made uploading a breeze.

  • ichha wrote on 17 January, 2010, 13:23

    Why ini_set() does not work. I want to increase the upload limit.

    Does it depends on PHP version

  • Ichha wrote on 17 January, 2010, 13:26

    Why ini_set() does not works.I want to increase the upload limit.
    Does this depends on PHP version

  • Sudeep Shrestha wrote on 2 February, 2010, 3:51

    i have uploaded a site ,its a huge with lots of pages more than 200, and lots of folder having files. i am using the filezilla. While using these i am not confirmed that if each file is being uploaded . Is there any way that i should confirm that all the files has been uploaded.

  • Gangadhara B S wrote on 8 February, 2010, 10:28

    Its working correctly, great post.
    Thanks a lot Dude

  • ProUKM wrote on 6 March, 2010, 6:36

    not work !

  • isabel wrote on 18 March, 2010, 10:50

    Hi everyone,

    I have the same problem: unable to upload files bigger than 12 MB;
    however:
    - I don’t have access to any of the ini-files
    - the hostingprovider tells me it’s not possible to ‘upgrade’ to allow the upload of more MB
    - adapting the .htaccess file with the code provided:
    php_value upload_max_filesize 20M
    php_value post_max_size 20M
    php_value max_execution_time 200
    php_value max_input_time 200
    returns an Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.
    - only 1 FTP user is available for this account (so I cannot provide this option either)..

    As per the hostingprovider, I have to “split the query in the notepad and then import the files” … is this an option? And if so, HOW do I have to do this?…

    Any help is really appreciated!
    thanks in advance ;)

  • ichha wrote on 24 March, 2010, 4:54

    Check what is the server type.If the server is windows server it works with .htaccess file but produces internal server error for the Linux server.

    For this you have to create the custom php.ini file and upload in the root folder.

    Create the php.ini file and put

    upload_max_filesize = 100M
    post_max_size = 100M
    output_buffering = on
    max_execution_time = 1000
    max_input_time = 1000
    memory_limit = 64M

    I think it works.

  • Ritesh wrote on 24 March, 2010, 6:17

    @Roshan- do you have any tips for uploading large file very fast.
    I have large 44MB .pdf, .xlsx and .csv. It seems like .csv uploading faster.
    Thanks!

  • bluepicaso wrote on 9 April, 2010, 12:13

    Really great, It worked like a charm

  • Prakash wrote on 20 April, 2010, 6:27

    Hi Roshan,
    I have tried the “set_time_limit(0);” to increase the execution time but it won’t worked.
    I feel your approach may work by putting it in .htaccess.
    any way thanks, will let you know about it.
    But why couldn’t i set it in .ini (remote server)….????
    like,
    ini_set(‘max_execution_time’, 7200); — Working
    ini_set(‘upload_max_filesize’, ’64M’); — Not working
    ini_set(‘max_input_time’, 7200); — Not working
    ini_set(‘memory_limit’, “64M”); — Not working

  • Prakash wrote on 20 April, 2010, 7:00

    Hey It’s not working buddy…..
    It is showing 500 Internal Server Error.

Trackbacks

  1. salutari, am rezolvat cu categoriile at artefect
  2. Uploading large(big) files in PHP using .htaccess « endo – explosion kiss
  3. KafeKafe » PHP Large File Upload With .htaccess
Copyright © 2013 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.