//判断远程文件 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; }
(1)使用fopen()函数,它要在allow_url_open开启的状态下,否则会报错。
$url = 'http://www.111cn.net /img/qrcode_for_phpddt.JPG'; if(@fopen($url, 'r')) { echo '文件存在'; } else { echo '文件不存在'; }
(2)get_headers取得服务器响应一个 HTTP 请求所发送的所有标头,效率较低,你可以测试下。
$url = 'http://www.111cn.net /img/qrcode_for_phpddt.JPG'; stream_context_set_default( array( 'http' => array( 'timeout' => 1, ) ) );
$headers = get_headers($url); if(preg_match('/200/',$headers[0])) { echo '文件存在'; } else { echo '文件不存在'; }
(3)file_get_contents()函数
$opts = array( 'http'=>array( 'timeout'=>3, ) ); $context = stream_context_create($opts); $resource = @file_get_contents('http://www.111cn.net /img/qrcode_for_phpddt.JPG', false, $context); if($resource) { echo '文件存在'; } else { echo '文件不存在'; }
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有