views:

128

answers:

9

I have a validation function I'm using inside of codeigniter.

function valid_image() {
    if ( ($_FILES["file"]["type"] != "image/jpeg") || ($_FILES["file"]["type"] != "image/gif")  ) {
    $this->form_validation->set_message('valid_image', 'Wrong file type..');

    return false;
} else {
    return true;
}

With just the "image/jpeg" part in the if statement it works fine. If I try to upload anything other than a jpg file it fails. If I run the code above, it fails with both a jpg or a gif file.

And before someone says "why not use the upload class," I can't. I'm saving my pics directly into MongoDB, so the upload class doesn't help much.

+5  A: 

You want AND not OR

If the file is jpg, then it's not gif, and you get the message.
If the file is gif, then it's not jpg, and you still get the message.

You have "file is not jpg OR file is not gif". Replace || with && and you will get the message only when "file is not jpg AND file is not gif".

JeffH
+1  A: 

Your if statement wrong. You should use '&&' instead of '||' (DeMorgan's law).

Macmade
+1  A: 

you need your condition to be

if ( ( $_FILES["file"]["type"] != "image/jpeg") 
       && ($_FILES["file"]["type"] != "image/gif")  ) {...}

if it's not a jpeg AND it's not a gif - it's not valid

meouw
+1  A: 

You need AND instead of OR

 if (($_FILES["file"]["type"] != "image/jpeg") && 
     ($_FILES["file"]["type"] != "image/gif"))
St. John Johnson
A: 

The statement as you have it guarantees that it is not jpeg and not gif. The problem with that is that if it is jpeg, it is still not gif, so it returns false. In fact, it always returns false since something can not be two things at once.

Change the || to && if that is your intent and it should help.

Matt
A: 

you want to use an and, not an or. In your above code, jpg fails because it's not a gif, and gif fails because it's not a jpg.

function valid_image() {
if ( ($_FILES["file"]["type"] != "image/jpeg") && ($_FILES["file"]["type"] != "image/gif")  ) {
$this->form_validation->set_message('valid_image', 'Wrong file type..');

return false;
} else {
return true;
}
GSto
A: 

Well if you try that statement with a .gif, it's going to say it's the wrong file because it checks the first 'or' first. Also, if you try and run a .jpg, it'll pass the first or but then fail the second, therefore it will still say it's invalid. Try making it '&&' instead of '||', that way it'll check and make sure it's one of the two every time.

Scott
+1  A: 

This doesn't really answer your question, but...

The $_FILE[blah]["type"] parameter is set by the web browser and as such is user data that can't be trusted.

You may want to use exif_imagetype($_FILES["file"]["tmp_name"]) instead to detect the true image type.

function valid_image() {
    $type = exif_imagetype($_FILES["file"]["tmp_name"]);
    if (($type != IMAGETYPE_GIF) && ($type != IMAGETYPE_JPEG)) {
        $this->form_validation->set_message('valid_image', 'Wrong file type..');
        return false;
    } else {
        return true;
    }
}

Edit: If the exif extension isn't installed, you can also do this:

$sizes = getimagesize($_FILES["file"]["tmp_name"]);

$sizes[2] will contain a value corresponding to one of the IMAGETYPE constants.

function valid_image() {
    $sizes = getimagesize($_FILES["file"]["tmp_name"]);
    if (($sizes[2] != IMAGETYPE_GIF) && ($sizes[2] != IMAGETYPE_JPEG)) {
        $this->form_validation->set_message('valid_image', 'Wrong file type..');
        return false;
    } else {
        return true;
    }
}
R. Bemrose
I ended up using your solution. thanks!
luckytaxi
+1  A: 

use && instead of ||

ezmia