Search code examples
phpexif

PHP: exif_imagetype() not working?


I have this extension checker:

$upload_name = "file";
$max_file_size_in_bytes = 8388608;
$extension_whitelist = array("jpg", "gif", "png", "jpeg");

/* checking extensions */
$path_info = pathinfo($_FILES[$upload_name]['name']);
$file_extension = $path_info["extension"];
$is_valid_extension = false;
foreach ($extension_whitelist as $extension) {
    if (strcasecmp($file_extension, $extension) == 0) {
        $is_valid_extension = true;
        break;
    }
}
if (!$is_valid_extension) {
    echo "{";
    echo        "error: 'ext not allowed!'\n";
    echo "}";
    exit(0);
}

And then i added this:

if (exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_GIF 
        OR exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_JPEG 
        OR exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_PNG) {
    echo "{";
    echo        "error: 'This is no photo..'\n";
    echo "}";
    exit(0);
}

As soon when I added this to my imageupload function, the function stops working. I dont get any errors, not even the one i made myself "this is no photo", what could be wrong?

Just checked with my host. They support the function exif_imagetype()


Solution

  • Here is the example with the switch usage:

    switch(exif_imagetype($_FILES[$upload_name]['name'])) {
        case IMAGETYPE_GIF:
        case IMAGETYPE_JPEG:
        case IMAGETYPE_PNG:
            break;
        default:
            echo "{";
            echo        "error: 'This is no photo..'\n";
            echo "}";
            exit(0);
    }
    

    Your script can be used too, but you have to put AND between conditions:

    if (exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_GIF 
            AND exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_JPEG 
            AND exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_PNG) {
        echo "{";
        echo        "error: 'This is no photo..'\n";
        echo "}";
        exit(0);
    }