
使用PHP检查远程文件是否存在
通常我们使用Curl来检查远程文件是否存在:
$ch = curl_init("http://www.baidu.com/favicon.ico");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($retcode == 200) {
// 文件存在
}
还有一种方法使用get_headers
$url = 'http://www.example.com/index.html';
$header = get_headers($url);
if ($header && $header[0] == 'HTTP/1.1 200 OK') {
//文件存在
}