PHP upload works for some and not for others!?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RobertSimmons
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 4:49 pm

PHP upload works for some and not for others!?

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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
RobertSimmons
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 4:49 pm

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

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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
RobertSimmons
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 4:49 pm

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

Post by RobertSimmons »

Thanks for your help. This is what I'm looking for.

Cheers,
Robert
Post Reply