判断网页是否存在,可以通过get_headers()函数。通过以下文件返回网页的状态,通过返回的状态来判断是否有网页文件。


$url = 'http://www.xxxx.com/firstPage/showDetail.asp?dwid=9634';
$headeraar = get_headers($url);
print_r($headeraar);

?>

如果以上网页存在,则返回:

Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: “3f80f-1b6-3e1cb03b”
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)

如果以上网页发生转向,则返回

Array
(
[0] => HTTP/1.1 302 Object moved

...

)

如果没有文件,则返回

Array
(
[0] => HTTP/1.1 404 Not Found

...

)

因此文件是否存在,只要判断$headeraar[0]的状态是否为“HTTP/1.1 200 OK”即可。

判断代码如下:


if(strpos($headeraar[0],'HTTP/1.1 200')===0){
        echo '
有文件';
    }else{
         echo '
没有文件';
    }

?>