简体   繁体   中英

PHP if statement doesn't work properly (paradoxical)

I've got a strange problem. I want to write a simple multiple image upload script in PHP. So there is an if statement I need for checking if the image has got the correct image type, which doesn't work as expected. The image I try to upload is a .jpg image. The "jpg" information is stored in the variable $mediaFileType, which is being checked by the if statement. Normally it should execute the script in the else statement now, but it always does the opposite and states that "jpg" != "jpg" = true. And it's obviously not. So what am I missing here?

if($mediaFileType != 'jpg' && $mediaFileType != 'png' && $mediaFileType != 'jpeg')
 {

    echo 'Wrong Media Type<br>';
    echo $mediaFileType;
    exit();

} else {
    ....
}

The website simply says:

Wrong Media Type
jpg

You can use:

if (!in_array(trim($mediaFileType), ['jpg', 'png', 'jpeg'])) {
    echo 'Wrong Media Type<br>';
    echo $mediaFileType;
    exit();
}

Your problem is probably a whitespace character

if (!in_array(trim($mediaFileType), ['jpg', 'png', 'jpeg']))
{
    echo 'Wrong Media Type<br/>';
    var_dump($mediaFileType);
    exit();
}
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM