PHP Increase Upload File Size Limit

by on June 11, 2010 · 28 comments· LAST UPDATED June 11, 2010

in

How do I increase upload file limit from 2MB to 10MB under Apache 2 UNIX / Linux web server?

Your php installation putting limits on upload file size. The default will restrict you to a max 2 MB upload file size. You need to set the following two configuration options:

  1. upload_max_filesize - The maximum size of an uploaded file.
  2. memory_limit - This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.
  3. post_max_size - Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size.
  4. There are two methods two fix this problem.

Method # 1: Edit php.ini

Edit your php.ini file (usually stored in /etc/php.ini or /etc/php.d/cgi/php.ini or /usr/local/etc/php.ini):
# vi /etc/php.ini
Sample outputs:

 
memory_limit = 32M
upload_max_filesize = 10M
post_max_size = 20M
 

Save and close the file. Restart apache or lighttpd web server:
# service httpd restart
OR
# service lighttpd restart

Method #2: Edit .htaccess

Edit .htaccess file in your root directory. This is useful when you do not have access to php.ini file. In this example, /home/httpd/html is considered as root directory (you can also create .htaccess file locally and than upload it using ftp / sftp / scp client):
# vi /home/httpd/html/.htaccess
Append / modify setting as follows:

 
php_value upload_max_filesize 10M
php_value post_max_size 20M
php_value memory_limit 32M
 

Save and close the file.

A Note About Suhosin (Optional)

This is not installed by default on many servers (latest version of Debian, Ubuntu, and FreeBSD does install Suhosin by default). Use phpinfo() to find out if suhosin enabled or not (create test.php):

 
<?php
   phpinfo();
?>
 

If you are using Suhosin which was designed to protect your servers against a number of well known problems in PHP applications and on the other hand against potential unknown vulnerabilities within these applications or the PHP core itself. You need to edit /etc/php.d/suhosin.ini to set correct memory and upload limit. As long scripts are not running within safe_mode they are free to change the memory_limit to whatever value they want.
suhosin.memory_limit=32M

See also:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 28 comments… read them below or add one }

1 Kevin June 18, 2010 at 4:12 pm

Hello,

Referring to these comments, What is the MAX file size in PHP? For example, can I set it at

upload_max_filesize = 200M
post_max_size = 200M
memory_limit = 300M

Thank you in advance.
Kevin

Reply

2 Sapan June 23, 2010 at 8:39 am

Thanks

I apply your method2 and it really works now..

Reply

3 kayo August 2, 2010 at 6:29 am

upload_max_filesize = 15M
memory_limit = 128M
post_max_size = 25M
I tried method 1 and 2 with these values but unable to upload 6mb of data file.

Reply

4 kayo August 2, 2010 at 6:31 am

I’m using these version:
Apache/2.2.3 (CentOS)
PHP Version 5.1.6

Reply

5 Yasar November 26, 2012 at 4:22 am

I am using WAMP and working fine.

Change the above three contents in both C:\wamp\bin\php\php5.2.6\php.ini and quick launch at the bottom of the desktop right click->PHP->php.ini

Check it KAYO.

Reply

6 yeswanth October 4, 2010 at 9:46 am

we have Apache server in our office and i dont have access to .ini or htaccess file.
cant we increase upload file size from our code itself?

Reply

7 JRM March 10, 2012 at 5:22 pm

I have the same setup… did you get an answer… I need access to the htaccess or php.ini but I don’t have permissions

is there are way to change it with scripts?

please let me know

Thanks

Reply

8 alreymark October 24, 2010 at 8:14 am

great thanks! really works well on my side. :)

Reply

9 Md. Ashraf Jahan April 4, 2011 at 5:28 pm

Its showing internal server error (500) when I use .htaccess

Reply

10 artiemartin March 14, 2012 at 6:07 pm

i have the same issue .. i added the code to the htaccess and it didn’t work .. my host said he’ll get back to me cause he doesn’t know why either I assume or he would of fixed it ..

Reply

11 Ash April 4, 2012 at 6:36 pm

I had this error when I created my .htaccess file using a program like dreamweaver. apparently you need to create it with work and make sure it has the correct encoding that your server uses.

Reply

12 shekhar April 28, 2011 at 9:46 am

thanx !!! it really worked…..

Reply

13 CSAdmin November 26, 2011 at 12:05 am

Thanks. Just needed help finding the php.ini file. Worked fine.

Reply

14 kamal January 12, 2012 at 7:11 am

please let me know also ,i am also uploading large file and have done all settings correctly but not able to upload file of large size

Reply

15 subna January 14, 2012 at 12:04 pm

thank u so much. whole day i spend 2 find out dis error finally after visiting 2 dis site i got d solution of my error. thnx :)

Reply

16 Jacob January 19, 2012 at 7:31 pm

What do you do if you don’t have that string in your php.ini file? Can I just add it at the end?

Reply

17 nixCraft January 20, 2012 at 6:16 am

Yes, you can add it at the end and reload / restart the webserver.

Reply

18 Jacob January 22, 2012 at 3:20 am

Thanks!

Reply

19 collin February 1, 2012 at 5:42 pm

I tried the .htaccess and .ini methods both made my apache threads 780 MB a peice. They are normaly 25MB. What’s up with that?

Reply

20 peerfly February 4, 2012 at 11:20 am

Thanks. I couldn’t upload my data base backup coz of this problem. Had to access the php.ini but only had to change post_max_size.

.htaccess method didn’t work. I don’t know why.

Reply

21 Kenny February 10, 2012 at 8:25 pm

Is there some syntax or directive section that this goes in? If I put simply what you have:
php_value upload_max_filesize 10M
php_value post_max_size 20M
php_value memory_limit 32M

Then I get a 500 error. If I stick it in tags, I get no error, but it doesn’t work. That’s probably because, in spite of all the postings, there’s no such Apache module as mod_php. So I don’t know what to do.
Any ideas? Thanks!

Reply

22 MerlinW April 14, 2012 at 2:54 pm

You have no rights to write to the target directory. Modify the user rights/attributes.

Reply

23 andy April 29, 2012 at 9:26 pm

All of these responses are inanely pathetic.
They all say yes i fixed it but no one says how.
Another waste of the internet

Reply

24 Hector April 30, 2012 at 5:44 pm

Very good article, was usefully… Thank you so much.

Reply

25 Fernando September 17, 2012 at 5:11 am

Hi guys,

Can you help me to configure this on my .htaccess ? My application is telling me this:

Upload stream is blocked by Suhosin, please add “upload” to suhosin.executor.include.whitelist

Regards.

Reply

26 adeniyi March 28, 2013 at 9:08 pm

thanks am very gratefull

Reply

27 George July 4, 2013 at 8:47 pm

This solution works. However, when I set the memory limit to 32MB, it causes my CSM (Drupal 7) to fail on certain pages (like module install/uninstall, adding content, etc.). Rather than trial and error, I just added the two lines to my main .htaccess:

php_value upload_max_filesize 10M
php_value post_max_size 20M

This was sufficient to increase the file upload size to 10MB.

Thanks!

Reply

28 Marcin September 4, 2014 at 12:08 pm

Thats very usefull, solved my issue. Thx.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: