≡ Menu

PHP Increase Upload File Size Limit

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:

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 30 comments… add one }

  • Kevin June 18, 2010, 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

  • Sapan June 23, 2010, 8:39 am

    Thanks

    I apply your method2 and it really works now..

  • kayo August 2, 2010, 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.

    • kayo August 2, 2010, 6:31 am

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

    • Yasar November 26, 2012, 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.

  • yeswanth October 4, 2010, 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?

    • JRM March 10, 2012, 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

  • alreymark October 24, 2010, 8:14 am

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

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

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

    • artiemartin March 14, 2012, 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 ..

      • Ash April 4, 2012, 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.

  • shekhar April 28, 2011, 9:46 am

    thanx !!! it really worked…..

  • CSAdmin November 26, 2011, 12:05 am

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

  • kamal January 12, 2012, 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

  • subna January 14, 2012, 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 :)

  • Jacob January 19, 2012, 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?

    • nixCraft January 20, 2012, 6:16 am

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

      • Jacob January 22, 2012, 3:20 am

        Thanks!

  • collin February 1, 2012, 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?

  • peerfly February 4, 2012, 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.

  • Kenny February 10, 2012, 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!

    • MerlinW April 14, 2012, 2:54 pm

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

  • andy April 29, 2012, 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

  • Hector April 30, 2012, 5:44 pm

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

  • Fernando September 17, 2012, 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.

  • adeniyi March 28, 2013, 9:08 pm

    thanks am very gratefull

  • George July 4, 2013, 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!

  • Marcin September 4, 2014, 12:08 pm

    Thats very usefull, solved my issue. Thx.

  • Abhishek Pandit January 14, 2015, 9:23 am

    Thanks. It worked.

  • vivek July 15, 2015, 11:46 am

    I have same issue setting done in php.ini but its not working how can I find.htaccess server having 3.5GB of memory need to increase memory as well I can upload upload upto 8MB file but not more than 8MB please guide I am new in Linux

Leave a Comment