1
$headers = get_headers("http://www.domain.com/image.jpg");

如何使用它确保文件是实际图像?

4

4 回答 4

4

一点也不。标头可能会或可能不会告诉您(通过Content-type),服务器认为这是什么 - 但没有什么能阻止您将例如myfile.zip放在网络服务器上,然后将其重命名为myfile.jpg. 网络服务器将提供它Content-type: image/jpeg,它肯定不是。

于 2012-05-13T14:34:48.567 回答
4

就像 Eugen Rieck 所说,不下载就无法确定它是否是图像。服务器始终可以更改 Content-Type 标头以使其看起来像图像。

但是,下载后,您可以使用此功能进行检查:

if (getimagesize('path/to/image.jpg')) {
    // image
}
else {
    // not an image
}

如果您仍然想使用 Content-Type,这应该可以工作:

$headers = array_change_key_case (get_headers ('http://example.com/exampleimage.jpg', 1) );
if (substr ($headers ['content-type'], 0, 5) == 'image') {
    // image
}
else {
    // not an image
}

array_change_key_case()用于使所有数组键小写,因此大小写没有区别。

于 2012-05-13T14:39:47.663 回答
1

在结果数组中查找“Content-Type”并检查它是否以“image/”开头

于 2012-05-13T14:37:32.117 回答
0

这个方法很简单,

$headers = get_headers( 'http://covers.openlibrary.org/b/isbn/9780141323367-S.jpg' );
$image_exist = implode(',',$headers);
if (strpos($image_exist, 'image') !== false) 
{
    echo 'Yups';
}
else 
{
    echo "Nopes";
}
于 2013-02-22T19:09:29.437 回答