Can someone tell me why, when selecting a psd file, the if statement in the php code passes as true and echos "image/vnd.adobe.photoshop"?
<?php
if (isset($_POST['submit'])) {
foreach ($_FILES["myimages"]["error"] as $key => $error) {
$tmp_name = $_FILES["myimages"]["tmp_name"][$key];
$name = $_FILES["myimages"]["name"][$key];
$imagetype = $_FILES['myimages']['type'][$key];
if ($imagetype == "image/jpeg" || "image/gif") {
echo $imagetype;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="<? echo basename(__file__); ?>">
<input type="file" name="myimages[]" multiple>
<input name="submit" type="submit" value="submit">
</form>
</body>
</html>
Notice that the boolean in the if-test (true or false) happens to be the same as the value we want to return. If the test value is true, we return true. If the test value is false, we return false.
An if statement does not evaluate whether the command inside its condition ran successfully. It will only check the value (in your case the return of your command) and cast it to a bool . Copy-Item does not return anything by default, and that's why your if statement is always false , because [bool]$null is $false .
returning true or false indicates that whether execution should continue or stop right there.
Because they don't represent equally convertible types/values. The conversion used by == is much more complex than a simple toBoolean conversion used by if ('true') . So given this code true == 'true' , it finds this: "If Type(x) is Boolean , return the result of the comparison ToNumber(x) == y ."
Because this is wrong
if( $imagetype == "image/jpeg" || "image/gif" ) { /*...*/ }
Should be
if( $imagetype == "image/jpeg" || $imagetype == "image/gif" ) { /*...*/ }
Or even
if( in_array($imagetype, ["image/jpeg", "image/gif"]) ) { /*...*/ }
That is, because non-empty string is considered true, so the IF condition was met.
This is because bitwise operator has lower priority than equality check. It goes like this:
You have
if ($imagetype == "image/jpeg" || "image/gif") {
Equality resolves to boolean first, and your expression becomes:
if (false || "image/gif") {
Non-empty strings are treated like true
when used in expression. That means we have
if (false || true) {
And the result of this, of course, is true
, so the if-block executes.
Your || statement is incorrect
$imagetype == "image/jpeg" ||
$imagetype == "image/gif"
What's happening is "image/gif" is returning true and that true is being or'd with the false returned by $imagetype == "image/jpeg". false || true == true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With