PHP:exif_imagetype() 不起作用?

发布于 2024-10-02 10:17:21 字数 1067 浏览 8 评论 0原文

我有这个扩展检查器:

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

当我将其添加到我的图像上传功能时,该功能就停止工作。我没有收到任何错误,甚至连我自己犯的“这不是照片”都没有,可能是哪里出了问题?

刚刚和我的主人核实过。他们支持函数 exif_imagetype()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-10-09 10:17:21

以下是开关用法的示例:

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

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);
}
厌倦 2024-10-09 10:17:21

有一种更好、更短的方法来获取图像类型,我目前正在使用它(无法确定它是否有效,因为 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 :(
}

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 :(
}
ヤ经典坏疍 2024-10-09 10:17:21
if (exif_imagetype($_FILES['image']['tmp_name']) != IMAGETYPE_JPEG 
    AND exif_imagetype($_FILES['image']['tmp_name']) != IMAGETYPE_PNG) 
{
enter code here
}

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文