Page 1 of 1

Safe and Secure File Uploader function

Posted: Mon Feb 05, 2007 10:48 pm
by blizzard_jdf
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.

Posted: Mon Feb 05, 2007 11:03 pm
by feyd
is_file() and is_executable() are file system level checks, not operating system level ones.. ;)

We've talked about uploading a lot, so seeking out previous talks of uploading may be a good idea.

Posted: Mon Feb 05, 2007 11:10 pm
by superdezign
You could try uploading (without putting it on the server) different types of file to get there exact $_FILES['whatever']['type'] and limit your uploading to those specific types

Note, however, that I've tried this and you get small things like image/jpeg and image/pjpeg (from Photoshop) and things like that, but it ensures you get the correct file types.

Posted: Tue Feb 06, 2007 10:32 am
by feyd
The type provided in $_FILES is not reliable at all. It is provided by the user-agent performing the upload and is not checked by PHP for validity.

Posted: Tue Feb 06, 2007 5:38 pm
by Ambush Commander
MIME magic is good for sniffing out the file type.