方法一:使用curl判断远程文件是否存在:
function remoteFileExists($url) {
$curl = curl_init($url);
//don\'t fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
//do request
$result = curl_exec($curl);
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
$exists = remoteFileExists(\'http://manongjc.com/favicon.ico\');
if ($exists) {
echo \'文件存在\';
} else {
echo \'文件不存在\';
}
方法二:使用fopen()判断远程文件是否存在
$file = \'http://manongjc.com/favicon.ico\';
$file_exists = (@fopen($file, \"r\")) ? true : false;
方法三:使用fopen()判断远程文件是否存在
function remote_file_exists($url){
return(bool)preg_match(\'~HTTP/1\\.\\d\\s+200\\s+OK~\', @current(get_headers($url)));
}
/* http://www.manongjc.com/article/1415.html */
$ff = \"http://manongjc.com/favicon.ico\";
if(remote_file_exists($ff)){
echo \"file exist!\";
}
else{
echo \"file not exist!!!\";
}
或
function is_url($url)
{
$array= get_headers($url);
$h= $array[0];
return( strlen($h==3) && ($h[2]==\'2\' || $h[2]==\'3\'));
}
注意:上面三种方法都是判断HTTP状态码(并没有下载文件),这样效率更高。
继续阅读与本文标签相同的文章
上一篇 :
DL时代的代码补全利器,效果远超语言模型
下一篇 :
一文告诉你全世界头部的开发者都在使用什么数据库
-
手机信号突然从“4G”变成“E”,是什么意思?客服给出答案
2026-05-14栏目: 教程
-
互联网架起“乌镇式生活”
2026-05-14栏目: 教程
-
微信才是内存“杀手”,别再乱清理,关闭这个功能,手机立马流畅
2026-05-14栏目: 教程
-
手机信号变成E是什么意思?看完专业人士给出的解释后,涨知识了
2026-05-14栏目: 教程
-
关于Word的四个隐藏办公小技巧,你可能一个也不知道,建议收藏!
2026-05-14栏目: 教程
