现在的位置: 首页 > 综合 > 正文

PHP 判断远程图片文件是否存在

2017年12月04日 ⁄ 综合 ⁄ 共 494字 ⁄ 字号 评论关闭

我推荐使用CURL方法

CURL 方法

<?php
$url2 = 'http://www.nowamagic.net/test.jpg';

$ch = curl_init(); 
$timeout = 10; 
curl_setopt ($ch, CURLOPT_URL, $url2); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

$contents = curl_exec($ch);
//echo $contents;
if (preg_match("/404/", $contents)){
	echo '文件不存在';
}
?>

curl_exec()执行完之后如果文件不存在,会返回如下信息:

HTTP/1.1 404 Not Found
Date: Tue, 14 Feb 2012 05:08:34 GMT
Server: Apache
Accept-Ranges: bytes
Content-Length: 354
Content-Type: text/html

抱歉!评论已关闭.