php判断远程文件是否存在的三种方法

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php判断远程文件是否存在的三种方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

方法一:使用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.ds+200s+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状态码(并没有下载文件),这样效率更高。

脚本宝典总结

以上是脚本宝典为你收集整理的php判断远程文件是否存在的三种方法全部内容,希望文章能够帮你解决php判断远程文件是否存在的三种方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: