Jump to content

Check if false using xor


tommy20

Recommended Posts

 

I want to check if a files is either jpeg or jpg and stop exit if it is something else. If tried this but it lets files that are not either jpeg or jpg load.

 

What can i do

 

if (!($uploaded_type=="image/jpg") xor !($uploaded_type=="image/jpeg" ) ) {

echo "You may only upload jpg picture files.<br>";

$ok=0;

exit();

}

 

 

Link to comment
Share on other sites

 

I want to check if a files is either jpeg or jpg and stop exit if it is something else. If tried this but it lets files that are not either jpeg or jpg load.

 

What can i do

 

if (!($uploaded_type=="image/jpg") xor !($uploaded_type=="image/jpeg" ) ) {

echo "You may only upload jpg picture files.<br>";

$ok=0;

exit();

}

 

When you're having trouble with propositional logic, it's often a good idea making a truth table

 

If we say that:

p: $uploaded_type=="image/jpg"

q: $uploaded_type=="image/jpeg"

 

Then the truth table looks like this:

 

[pre]p | q | ~p ⊕ ~q

---------------

T | T |    F

T | F |    T

F | T |    T

F | F |    F[/pre]

Link to comment
Share on other sites

And if you work out a truth table using OR you will find the it won't work either (because negative logic is being used in the tests, you would need to use AND.) Which is why it is usually best to test for allowed values and do something if a value is one of the possible allowed values.

Link to comment
Share on other sites

How about something easy to scale out?

 

$allowedTypes = array(
  "image/jpeg",
  "image/jpg",
);

if(!in_array($uploaded_type,$allowedTypes) {
  echo "You may only upload jpg picture files.<br>";
} 

 

 

Link to comment
Share on other sites

That doesn't do anything

Probably because of the syntax error, a missing ), try:

$allowedTypes = array(
  "image/jpeg",
  "image/jpg",
);

if(!in_array($uploaded_type,$allowedTypes)) {
  echo "You may only upload jpg picture files.<br>";
} 

Link to comment
Share on other sites

And unless stated otherwise, all code that is posted comes with an implied disclaimer that it is an untested example showing how you can do something, not that it is actual, final, tested code that can be directly used for any particular purpose. *

 

[* - kind of the same disclaimer that MS ships with its' products, that this is not actual, final, tested code that can be used for any particular purpose.]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.