tags:

views:

52

answers:

5

I am trying to test if the uploaded file is the image type I want. If it isn't a gif,jpeg, png, it should echo "Problem". But when I execute this code, it always says there's a problem. What's wrong with my if statement?

$uploadfile_type=$_FILES['userfile']['type'];

    if ( ($uploadfile_type !='image/gif') || ($uploadfile_type !='image/jpeg') 
    || ($uploadfile_type !='image/png'))  
    {
        echo 'Problem: file is not a gif or jpeg or png!';
        exit;
    }

This code works when I am only checking one type of image. Ex: if($uploadfile_type !='image/gif') --> this statement would work but when I add a OR it doesn't.

+5  A: 

You're using OR (||) when you should be using AND (&&). You want to say there's a problem if it's not a GIF and it's not a JPG and it's not a PNG. With OR, the only way it wouldn't say there was a problem is if you had some sort of file that managed to be all 3 types at once.

Chad Birch
Wow, thank you! Works now.But why would it be wrong to say "if the image isn't a gif OR the image isn't a jpg OR the image isn't a png"?If it was "and", why won't it be saying "it has to be a gif and a jpg and a png?Sorry, for these simple questions--i'm new
ggfan
Your code says : we should print an error when the image is a gif or when the image is a jpeg or when the image is a png. In other words : print an error whatever the type of the image !
Jerome WAGNER
@ggfan: It just requires a different way of thinking about it. The code was saying, "print an error if any of the following are true: it isn't a GIF, it isn't a JPG, it isn't a PNG". What you have to realize though, is that even if your image is a GIF, it also isn't a JPG and it isn't a PNG. So two of the conditions are still triggered, even in a "good" case.
Chad Birch
mmm makes clear sense now. gracias!
ggfan
A: 

You need to use AND instead of OR:

if ( ($uploadfile_type !='image/gif') && ($uploadfile_type !='image/jpeg')
  && ($uploadfile_type !='image/png'))  

OR will always fail since a file will only ever be of a single type.

Justin Ethier
A: 

Chad's answer is right.

If you want to shorten the code and make it prettier:

if (!in_array($uploadfile_type, array('image/gif', 'image/jpeg', 'image/png'))
{
  // bad
}
Coronatus
+1  A: 

You should define a list of accepted mimetypes :

$accepted_types = array(
    'image/gif',
    'image/jpeg',
    'image/png',
);

$uploadfile_type=$_FILES['userfile']['type'];

if (!in_array($uploadfile_type, $accepted_types))
{
    echo 'Problem: file is not a gif or jpeg or png!';
    exit;
}

I hope this will help, Jerome Wagner

Jerome WAGNER
+2  A: 

There's a riddle that goes:

I have two coins in my pocket. Their total value is thirty cents. One of them is not a nickel. What are they?

The answer is that they are a quarter and a nickel - the quarter satisfies the condition that "one of them is not a nickel". This is the kind of problem you are facing. Every file is not a gif or is not a jpeg - even the gifs and jpegs. The gifs are not jpegs, and the jpegs are not gifs. So, as others have said, you need to use AND instead of OR.

Carl Manaster