復制代碼 代碼如下:
 
//判斷遠程文件 
function check_remote_file_exists($url) 
{ 
$curl = curl_init($url); 
// 不取回數據 
curl_setopt($curl, CURLOPT_NOBODY, true); 
// 發送請求 
$result = curl_exec($curl); 
$found = false; 
// 如果請求沒有發送失敗 
if ($result !== false) { 
// 再檢查http響應碼是否為200 
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
if ($statusCode == 200) { 
$found = true; 
} 
} 
curl_close($curl); 
return $found; 
} 
復制代碼 代碼如下:
 
//默認效果 
print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif")); 
結果: 
Array 
( 
[0] => HTTP/1.1 200 OK 
[1] => Date: Thu, 02 Jun 2011 02:47:27 GMT 
[2] => Server: Apache 
[3] => P3P: CP=" OTI DSP COR IVA OUR IND COM " 
[4] => Set-Cookie: BAIDUID=7F6A5A2ED03878A7791C89C526966F3A:FG=1; expires=Fri, 01-Jun-12 02:47:27 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1 
[5] => Last-Modified: Thu, 20 Jan 2011 07:15:35 GMT 
[6] => ETag: "65e-49a41e65933c0" 
[7] => Accept-Ranges: bytes 
[8] => Content-Length: 1630 
[9] => Cache-Control: max-age=315360000 
[10] => Expires: Sun, 30 May 2021 02:47:27 GMT 
[11] => Connection: Close 
[12] => Content-Type: image/gif 
) 
//加參數1的效果 
print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif", 1)); 
結果: 
Array 
( 
[0] => HTTP/1.1 200 OK 
[Date] => Thu, 02 Jun 2011 02:49:28 GMT 
[Server] => Apache 
[P3P] => CP=" OTI DSP COR IVA OUR IND COM " 
[Set-Cookie] => BAIDUID=4D875812FC482C0ADE4F5C17068849EE:FG=1; expires=Fri, 01-Jun-12 02:49:28 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1 
[Last-Modified] => Thu, 20 Jan 2011 07:15:35 GMT 
[ETag] => "65e-49a41e65933c0" 
[Accept-Ranges] => bytes 
[Content-Length] => 1630 
[Cache-Control] => max-age=315360000 
[Expires] => Sun, 30 May 2021 02:49:28 GMT 
[Connection] => Close 
[Content-Type] => image/gif 
) 
復制代碼 代碼如下:
 
function check_remote_file_exists($url) 
{ 
$curl = curl_init($url); 
// 不取回數據 
curl_setopt($curl, CURLOPT_NOBODY, true); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); //不加這個會返回403,加了才返回正確的200,原因不明 
// 發送請求 
$result = curl_exec($curl); 
$found = false; 
// 如果請求沒有發送失敗 
if ($result !== false) 
{ 
// 再檢查http響應碼是否為200 
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
if ($statusCode == 200) 
{ 
$found = true; 
} 
} 
curl_close($curl); 
return $found; 
} 
$exists = check_remote_file_exists('http://www.baidu.com/img/baidu_sylogo1.gif'); 
echo $exists ? '存在' : '不存在'; 
$exists = check_remote_file_exists('http://www.baidu.com/test.jpg'); 
echo $exists ? '存在' : '不存在'; 
新聞熱點
疑難解答