Karem
Karem

Reputation: 18103

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()

Upvotes: 0

Views: 12544

Answers (3)

Sahan Pasindu Nirmal
Sahan Pasindu Nirmal

Reputation: 441

if (exif_imagetype($_FILES['image']['tmp_name']) != IMAGETYPE_JPEG 
    AND exif_imagetype($_FILES['image']['tmp_name']) != IMAGETYPE_PNG) 
{
enter code here
}

You can use like that. Hope this is will be helpful

Upvotes: 0

aesede
aesede

Reputation: 5703

There's a better, shorter way to get image types, which I'm currently using (can't be sure if it will works as it is not documented in PHP.net as image_type_to_mime_type() can also take an integer as input):

function get_image_type( $image_path_or_resource ) {
    $img = getimagesize( $image_path_or_resource );
    if ( !empty( $img[2] ) ) // returns an integer code
        return image_type_to_mime_type( $img[2] );
    return false;
}

echo get_image_type( 'somefile.gif' );
// image/gif
echo get_image_type( 'path/someotherfile.jpg' );
// image/jpeg

$image = get_image_type( 'somefile.gif' );
if ( !empty( $image ) ){
    // fine, let's do some more coding
} else {
   // bad image :(
}

Upvotes: 0

Marin Purgar
Marin Purgar

Reputation: 189

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);
}

Upvotes: 7

Related Questions