$headers = get_headers("http://www.domain.com/image.jpg");
如何使用此确保文件是实际图像?
答案 0 :(得分:4)
完全没有。标题可能会或可能不会告诉您(通过Content-type
),服务器认为这是什么 - 但没有什么阻碍您放置,例如在网络服务器上myfile.zip
,然后将其重命名为myfile.jpg
。网络服务器将使用Content-type: image/jpeg
来提供服务,但肯定不是。{/ p>
答案 1 :(得分:3)
就像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()
用于使所有数组键小写,因此情况不会有所作为。
答案 2 :(得分:1)
在结果数组中查找“Content-Type”并检查它是否以'image /'
开头答案 3 :(得分: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";
}