您的位置:首页 > 编程语言 > PHP开发

PHP判断远程文件是否存在

2016-07-20 10:38 615 查看
函数描述及例子

<?
//测试代码
$str_url = 'http://127.0.0.2/viewarticle.php?id=119617';
$exits = remote_file_exists($str_url);
echo $exists ? "Exists" : "Not exists";
?>


方法一(需要开启allow_url_fopen):

方法二(需要服务器支持Curl组件):

<?php
function check_remote_file_exists($url) {
$curl = curl_init($url); // 不取回数据
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // 发送请求
$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;
}

$url = "http://cn.wordpress.org/wordpress-3.3.1-zh_CN.zip";
echo check_remote_file_exists($url); // 返回1,说明存在。

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: