Help on Logic in IF statement

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
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Help on Logic in IF statement

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Help on Logic in IF statement

Post by Benjamin »

Depending on the mime type can result in security vulnerabilities. I would use getimagesize() to verify that it is an image.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Help on Logic in IF statement

Post 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.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Re: Help on Logic in IF statement

Post 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;
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Help on Logic in IF statement

Post by RobertGonzalez »

Where did you set $upload_type in your previous code?
Post Reply