Uploading large(big) files in PHP using .htaccess
Posted on January 15, 2008
Filed Under htaccess, php
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: 51% [?]
If you like this post then please subscribe to my full RSS feed . You can also subscribe by email and have new posts sent directly to your inbox.And, You can also follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» Prevent Directory Listing using .htaccess
» Absolute path and Relative path file inclusion in PHP
» Blogger Sucks, Wordpress Wowww!!
» Change default directory page using .htaccess
Comments
33 Responses to “Uploading large(big) files in PHP using .htaccess”
Leave a Reply





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.
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”.
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.
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.
hi jcapinc….ini_set() function is no longer useful when uploading the larger files in php so better to use .htaccess approach.
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”.
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?
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).
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.
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…..
@Roshan:
Passing 0 to max_execution_time implies unlimited script execution time.
Thanks Jahuf for pointing out my fault, sorry anandsharma Jahuf is right….
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/
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.
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
try phpinfo() you can get lots of info regarding various modules loaded and upload limit of your server. And furthermore check your query again..
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.
.htaccess doesnot do any effect in awardspace.com’s free hosted account.The server should allow the overriding.
Great, It helped me so far.And i liked the approach too.
Thanks sheraz…….
is it possible to upload 500mb file using .htaccess process in server?
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.
does your server support over-riding? you server must be configured to AllowOverride All in the configuration file of apache..check this once
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
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.
[...] http://roshanbh.com.np/2008/01/uploading-larger-files-in-php.html [...]
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.
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..
I exactly couldn’t understand what to fix ? And furthermore , you can change each and every setting of php.ini in your dedicated server.
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.
What is the error you may getting ? You may write the hosting provider regarding the problem of the issue, they may also help you..
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.
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.