How to set the limits of File Uploads and POST in PHP/Apache

Blog: Tutorials Blog
In this tutorial we are going to see how to change the limits set by PHP, for POST and File Uploads. It is always important to understand and have control of your PHP configuration. By default, PHP has these values set very low, but in most cases this would cause us problems. If for example we have a website that runs a CMS or App and we need to upload plugins and other kinds of files, such as images, from the browser, we need to raise the default values.

Default Values

post_max_size = 8M
upload_max_filesize = 2M

The post_max_size and upload_max_filesize are directives and are in a way related. The upload_max_filesize is the max limit of a single file upload, while the post_max_size is the max limit of the entire body of a request, that may include multiple files. This means that if we want to upload an image that is 5 megabytes, we need both the upload_max_filesize and post_max_size to have a higher value than 5M. In a similar way, if we have a plugin ready to install to our CMS and it's size is 8 megabytes, then we need these values to be set higher than the filezise of the archive we need to install/upload.

Lets see what the PHP manual says about these directives.

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. Generally speaking, memory_limit should be larger than post_max_size. PHP allows shortcuts for byte values, including K (kilo), M (mega) and G (giga). PHP will do the conversions automatically if you use any of these.

upload_max_filesize

The maximum size of an uploaded file.

Access and edit the php.ini

The recommended way is to set the desired values for these directives in your php.ini if your host gives you access to it. Hosting services often provide a custom php.ini file in your account's home directory, or through a control panel. Note that some hosts don't allow you to change some of these values. If you are not sure or you don't know where to look, contact your host and ask.

The way to set them in the php.ini is as in this example:
post_max_size = 20M
upload_max_filesize = 20M

Notice the capital M that follows the two digits without a space in between (M stands for megabytes). You can have K (kilo), M (mega) and G (giga). Remember to look for these directives in the php.ini file first and not set them twice at random places.


No access to php.ini

In the case that your server runs PHP as an Apache module and you have permission by your host's configuration to change these values using .htaccess, then you can set them in an .htaccess file (not recommended) as follows:
php_value post_max_size 20M
php_value upload_max_filesize 20M

Note that the php_value Apache directive is provided by the mod_php module. If you don't run PHP as an Apache module (i.e., it runs as FastCGI) then php_value directive will not be defined. If you get a 500 Internal Server Error, then you do not have permission to set these values using an .htaccess file.


Server Configuration Reload

If you are on a shared hosting environment, you may need to wait for a little while before your changes get applied. If you run your own server, you may need to reload Apache depending on where you made the change. On Debian based systems such as Debian and Ubuntu, run this in the shell:
service apache2 reload

You do not need a complete service restart.

Note: You do not need to raise these values too high except if you plan to upload movies via a php script. Do not confuse an FTP upload with this -- it is a different server that is going to answer the request, a WEB server in this case, not an FTP server.