方法一(需要开启allow_url_fopen):
3 | $fileExists = @ file_get_contents ( $url , null, null, -1, 1) ? true : false; |
方法二(需要服务器支持Curl组件):
02 | function check_remote_file_exists( $url ) { |
03 | $curl = curl_init( $url ); |
04 | curl_setopt( $curl , CURLOPT_NOBODY, true); |
05 | curl_setopt( $curl , CURLOPT_CUSTOMREQUEST, 'GET' ); |
06 | $result = curl_exec( $curl ); |
08 | if ( $result !== false) { |
11 | $statusCode = curl_getinfo( $curl , CURLINFO_HTTP_CODE); |
12 | if ( $statusCode == 200) { |
22 | echo check_remote_file_exists( $url ); |
09 | function remote_file_exists( $url_file ){ |
11 | $url_file = trim( $url_file ); |
12 | if ( empty ( $url_file )) { return false; } |
13 | $url_arr = parse_url ( $url_file ); |
14 | if (! is_array ( $url_arr ) || empty ( $url_arr )){ return false; } |
17 | $host = $url_arr [ 'host' ]; |
18 | $path = $url_arr [ 'path' ] . "?" . $url_arr [ 'query' ]; |
19 | $port = isset( $url_arr [ 'port' ]) ? $url_arr [ 'port' ] : "80" ; |
22 | $fp = fsockopen ( $host , $port , $err_no , $err_str ,30); |
23 | if (! $fp ){ return false; } |
26 | $request_str = "GET " . $path . "HTTP/1.1\r\n" ; |
27 | $request_str .= "Host:" . $host . "\r\n" ; |
28 | $request_str .= "Connection:Close\r\n\r\n" ; |
31 | fwrite( $fp , $request_str ); |
32 | $first_header = fgets ( $fp , 1024); |
36 | if (trim( $first_header ) == "" ){ return false;} |
37 | if (!preg_match( "/200/" , $first_header )){ |
1 | <P><STRONG>函数描述及例子</STRONG></P> |
3 | <PRE class = "brush:php; toolbar: true; auto-links: true;" ><? |
6 | $exits = remote_file_exists( $str_url ); |
7 | echo $exists ? "存在" : "不存在" ; |
附送一个JS的判断
01 | < script language = "网页特效 " > |
04 | var xmlhttp = new activexobject( "microsoft.xmlhttp "); |
05 | xmlhttp.open( "get ",url,false); |
07 | if (xmlhttp.readystate==4) |
08 | alert((xmlhttp.status==200)? "文件存在 ": "文件不存在 "); |
12 | < button onclick = "geturl(file.value) " > 检测地址 </ button > |