PHP:exif_imagetype()不起作用?

时间:2010-11-11 13:45:44

标签: php exif

我有这个扩展检查器:

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

然后我补充说:

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

当我将其添加到我的imageupload函数时,该函数停止工作。我没有得到任何错误,甚至不是我自己制作的“这不是照片”,可能是什么错误?

刚与我的主人核对过。它们支持exif_imagetype()

函数

3 个答案:

答案 0 :(得分:7)

以下是交换机用法的示例:

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

您的脚本也可以使用,但您必须在条件之间放置AND:

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

答案 1 :(得分:0)

有一种更好,更短的方式来获取我正在使用的图像类型(不能确定它是否可行,因为它没有在PHP.net中记录,因为image_type_to_mime_type()也可以采用整数作为输入):

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 :(
}

答案 2 :(得分:0)

checked

你可以这样使用。希望这会有所帮助