Finfo_file()为某些图像返回错误的mime类型


finfo_file() returns wrong mime type for some images?

我允许在我的网站上上传图像,需要获得上传文件的文件类型,以便我可以检查该类型是否被列入白名单并允许上传。

我一直在使用finfo_file()来做这件事,但它似乎在所有情况下都不起作用。下面是我的代码:

// get the mime type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime  = finfo_file($finfo, $filePath);
echo $mime;
http://ecx.images-amazon.com/images/I/41q8koaxM0L.jpg

试着在上面的图像上运行上面的代码,它会显示MIME类型是application/octet-stream,而它显然是一个JPG文件。

为什么finfo_file()不能与测试图像一起工作?我怎样才能解决问题,以便我能准确地获得文件类型?

或者,您也可以将exif_imagetype()image_type_to_mime_type()结合使用。例子:

function get_mime($url) {
    $image_type = exif_imagetype($url);
    return image_type_to_mime_type($image_type);
}
echo get_mime('http://ecx.images-amazon.com/images/I/41q8koaxM0L.jpg'); // image/jpeg