Page 1 of 1

PHP upload works for some and not for others!?

Posted: Mon Aug 31, 2009 5:05 pm
by RobertSimmons
I have a simple upload script installed in a website. The code checks file type and size before uploading the file to the appropriate directory. Problem I'm having is the script works perfectly well when I test it but returns an error when a client uses it even though their file meets to the requirements. The client emailed me the file that was rejected and I uploaded the same file through the same script without it being rejected. Code is:

Code: Select all

<?php
 
$target_path = "../images/";
 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
 
$limit_size=5000000;
 
$file_size = $_FILES['uploadedfile']['size'];
 
 
if (
(
($_FILES['uploadedfile']['type'] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpg")
|| ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
)
&& (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
&& ($file_size <= $limit_size)
) 
 
{
 echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been successfully uploaded.</p>";
} 
 
else{
    echo "<p><b>There was an error uploading the file,</b> please try again! Make sure your file is under 5 Mbs and is in one of the following formats: jpeg, gif or png.</p>";
}
 
?>
Why does this work from some computers and not others? Please help. Thanks.

Re: PHP upload works for some and not for others!?

Posted: Mon Aug 31, 2009 5:14 pm
by Darhazer
The problem is that you are checking the type browser told you the file is, and it can be different from browser to browser (check your code with IE, Firefox and Opera). Your browser reports 'image/jpeg', but client's browser reports something different.
If you are uploading images, the proper way to check the type is the getimagesize() function

Re: PHP upload works for some and not for others!?

Posted: Mon Aug 31, 2009 5:59 pm
by RobertSimmons
How can I correct this? Or should I say, what is the proper way to check files?

I have another upload link that checks for document types and not images... I'd like to use a similar method on both.

??? What is perplexing is that the client could use this method before with no problem and just recently started having problems.

Re: PHP upload works for some and not for others!?

Posted: Tue Sep 01, 2009 2:10 pm
by Darhazer
Change in client's browser (or some browser plugins maybe)?

For images:

Code: Select all

$result = getimagesize($_FILES['uploadedfile']['tmp_name']);
if ($result == false) { // error;
}
list($width, $height, $type) = $result;
 
Then check the $type variable.
More info here:
http://php.net/manual/en/function.getimagesize.php

Re: PHP upload works for some and not for others!?

Posted: Tue Sep 01, 2009 7:43 pm
by RobertSimmons
Thanks for your help. This is what I'm looking for.

Cheers,
Robert