【问题标题】:How to check the uploaded file for image如何检查上传的文件中的图像
【发布时间】:2013-03-31 05:45:20
【问题描述】:

我知道 PHP 中带有 getimagesizeFileinfo 扩展名的方法,但它们都需要一些库(GDFileinfo)。

我发现此代码用于从提供的文件中检测图像:

function is_image($image_path)
{
    if (!$f = fopen($image_path, 'rb'))
    {
        return false;
    }

    $data = fread($f, 8);
    fclose($f);

    // signature checking
    $unpacked = unpack("H12", $data);
    if (array_pop($unpacked) == '474946383961' || array_pop($unpacked) == '474946383761') return "gif";
    $unpacked = unpack("H4", $data);
    if (array_pop($unpacked) == 'ffd8') return "jpg";
    $unpacked = unpack("H16", $data);
    if (array_pop($unpacked) == '89504e470d0a1a0a') return "png";

    return false;
}

它安全吗?要不要用?

【问题讨论】:

  • 使用 getimagesize() 是最好的选择。你的代码只是检查图片的头部,如果图片中间注入了恶意怎么办?

标签: php image filter detect


【解决方案1】:

我引用了 getimagesize 函数的 PHP 文档:-

此功能不需要GD图像库。

所以你应该可以使用它。

【讨论】:

  • 哦。谢谢你。没注意到。
猜你喜欢
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 2017-06-11
  • 2013-01-25
  • 1970-01-01
  • 2017-12-15
  • 2019-09-04
  • 1970-01-01
相关资源
最近更新 更多