Page 1 of 1

Help on Logic in IF statement

Posted: Mon Feb 11, 2008 12:18 pm
by irishmike2004
Greetings All:

I am having a real problem with the logic of the following IF statement:

Code: Select all

if (($uploaded_type != "image/jpeg") || ($uploaded_type != "image/png")) {
echo "You may only upload JPEG or PNG files.<br><br>";
$ok=0;
}
What I am trying to do is if the type of image is JPEG or PNG it is acceptable, otherwise throw an error.

The statement in the form above does not work... hope someone can help me fix the logic.

Thanks,

Mike

Re: Help on Logic in IF statement

Posted: Mon Feb 11, 2008 12:22 pm
by Benjamin
Depending on the mime type can result in security vulnerabilities. I would use getimagesize() to verify that it is an image.

Re: Help on Logic in IF statement

Posted: Mon Feb 11, 2008 12:23 pm
by JAM
Use and instead of or. Your way will always result in false, as both cannot be the same at the same time.

Code: Select all

    if ($uploaded_type != "image/jpeg" && $uploaded_type != "image/png") {
        echo "You may only upload JPEG or PNG files.<br><br>";
        $ok=0;
    }
If you want to use or you would need to use == instead of !=. Hope I made sence.

Re: Help on Logic in IF statement

Posted: Mon Feb 11, 2008 12:48 pm
by irishmike2004
for some reason it still is not working.

I tried the && idea and no dice.

Again the idea is that if the filetype is JPEG or PNG upload it, otherwise, give an error.

Appreciate the help.

Got it to work. Here is the code that did it... Though I am not quite sure WHY this worked as opposed to the other way... someone can explain it if they like ;-)

Code: Select all

if (($_FILES['cafile']['type'] != "image/jpeg") && ($_FILES['cafile']['type']!= "image/png")) {
echo "You may only upload JPEG or PNG files.<br><br>";
$ok=0;
}

Re: Help on Logic in IF statement

Posted: Mon Feb 11, 2008 1:39 pm
by RobertGonzalez
Where did you set $upload_type in your previous code?