View Full Version : Safe and Secure File Uploader function

02-06-2007, 04:49 AM
I am developing a method which will take a posted file, perform a series of checks and then write the file to the server (Linux) if it passes all of the checks.

Currently I perform the following checks:

Check to make sure the filesize is not too big
Check the files extension

Not quite sure what else to put. I have a very fast server so it okay if it performs a lot of checks. better safe than sorry. I was going to check the mime type but my server does not have that extension installed. I was also thinking of using the is_file() function to make sure a proper file is uploaded and the is_executable() function to make sure that no executable files are uploaded (Currently I am only uploading images).

I'd like to make my upload function very robust so I am sure that their are other checks which need to be performed. Also I have heard that apache can also report errors on file uploads. Though I am not sure how to do this.

02-06-2007, 08:49 AM
This is what I use for uploading images on my website and it seems to work just fine..

// include all allowed mime types here
$allowed = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
// check that uploaded type is allowed.
if( !array_key_exists( $_FILES['filename']['type'], $allowed ) ) {
} else {
echo "File type is allowed";

I also check that the file size is not zero (or below zero for that matter ) or higher than a given size.

Further, I use chmod (http://uk2.php.net/chmod), to control the file properties once it has been uploaded to the server. Personally, I make it only readable, not executable and not writeable.

As for Apache reporting errors...I have not encountered any and I don't think it is Apache that would be throwing any errors...since PHP is the process that handles file uploads in this case. However, I encourage you to check on this.


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum