Increase Upload File Size Limit in PHP

The default file size upload limit in php is limited to 2MB. When uploading files to your VPS or dedicated server via a web based content management system or document management system, you may want to increase this file size limit to accomodate larger files.

Modify Three Settings

Set the following three 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.

Two Methods to Change the Settings

Depending on your level of access to the server, you can modify the settings above in two different methods.

Method #1: Edit php.ini

If you are running a (vps) virtual private server or dedicated server, your root account will have access to the php.ini file. This can be edited using the Webmin installation on your server or via SSH

a) Edit php.ini via Webmin

  1. In the navigation, go to Others > PHP Configuration
  2. Click Manage next the Global PHP configuration file
  3. Click Resource Limits
  4. Modify the settings based on the three settings above. Your configuration might look like this:
Memory and transfer limit options
Maximum memory allocation Maximum HTTP POST size
Maximum file upload size Maximum execution time  seconds
Maximum input parsing time  seconds

b) Edit php.ini via SSH

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 settings:

memory_limit = 128M
upload_max_filesize = 20M
post_max_size = 30M

Save and close the file. Restart Apache web server:
# service httpd restart

Method #2: Edit .htaccess

Edit .htaccess file in the root directory of your website. This is useful when you do not have access to the php.ini on a server. You can create the .htaccess file locally and upload it via FTP.

Append / Modify settings:

php_value upload_max_filesize 20M
php_value post_max_size 30M
php_value memory_limit 128M