kmcm

Web Development with PHP, HTML, CSS, & JavaScript

Allowing large file uploads in PHP

Tuesday 9th February 2010

When creating an interface for uploading files to a server through a website, there are limitations in place in the PHP configuration that you must be aware of, in particular post_max_size and upload_max_filesize. Whilst recently working on a content management system, I came across a problem, in that files larger than 8MB where not being uploaded correctly through a form.

The CMS used a form with the post method to upload the files, and then used PHP FTP functions to place the file on the server. This problem was initially a little confusing, as when I check the PHP configuration, the value of upload_max_filesize was set to 32MB. Although this specifies the max value of allowed uploads, it doesn’t take into account the method used for uploads. When using a form with the post method to upload files, the post_max_size is the controlling value. And sure enough, on this occasion it was set to 8MB.

This means that while you’re upload_max_filesize can be large as you like, you are bound to the limit of the method you use. Obviously it’s not acceptable to allow file uploads only up to 8MB in a CMS (especially when music and video is involved), so I looked into ways of changing this, and .htaccess provided me with an easy solution.

A few simple lines in the .htaccess file can override the default server configuration for a number of things, including the post_max_size and upload_file_size.:

php_value upload_max_filesize 40M
php_value post_max_size 40M

The above code increases the limit for both values to 40mb, which I would deem to be more than enough. Although that’s not it quite finished yet. With the larger allowed file sizes comes longer execution time, and we don’t want the process to timeout before the upload complete. Looking at my php config I saw that the max_execution_time and max_input_time were set to 30 and 60 seconds respectively. Although this is a good limit, stopping looping scripts from running, if we were to upload bigger files, it may well take longer for the process to complete. So, again in the .htaccess file, we can override the master values:

php_value max_execution_time 200
php_value max_input_time 200

Altogether , the above 4 lines of code added to the .htaccess simply allow for larger uploads through a html form,  and the values of each can be change to whatever limit you like.

 

Filed under: