我推荐使用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