您的位置:首页 > 编程语言 > PHP开发

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

2013-05-27 14:04 706 查看
我推荐使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: