Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-06-2007, 04:49 AM   PM User | #1
j83folino
New to the CF scene

 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
j83folino is an unknown quantity at this point
Safe and Secure File Uploader function

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.
j83folino is offline   Reply With Quote
Old 02-06-2007, 08:49 AM   PM User | #2
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
This is what I use for uploading images on my website and it seems to work just fine..

PHP Code:
// 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 ) ) {
echo 
"ERROR....FILE NOT 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 , 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.

Cheers,
Ess
ess is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 PM.


Advertisement
Log in to turn off these ads.