您好,欢迎访问一九零五行业门户网

PHP get_headers函数判断远程文件是否存在_PHP教程

以前我有讲过程关于php判断远程文件是否存在的文章,那里都介绍利用fopen,sockt,curl函数来实现检查远程文件是否存在了,下面我再介绍利用 get_headers来检查远程文件是否存在,有需要了解的朋友可参考。 
先来简单了解get_headers()函数
get_headers() 返回一个数组,包含有服务器响应一个 http 请求所发送的标头。
get_headers:发送服务器响应http请求
get_headers(字符串url[链接格式])
get_headers()以数组的形式返回服务器http请求。如果执行失败,将返回false和一个错误的水平e_warning》。
可选参数设置为1,get_headers()能分析系统的响应速度和集数组中的键。
注意:使用该函数需要把 php.ini里面的allow_url_fopen = on,才能使用

 代码如下 复制代码
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
返回值
array
(
    [0] => http/1.1 200 ok
    [1] => date: sat, 29 may 2004 12:28:13 gmt
    [2] => server: apache/1.3.27 (unix)  (red-hat/linux)
    [3] => last-modified: wed, 08 jan 2003 23:11:55 gmt
    [4] => etag: 3f80f-1b6-3e1cb03b
    [5] => accept-ranges: bytes
    [6] => content-length: 438
    [7] => connection: close
    [8] => content-type: text/html
)
array
(
    [0] => http/1.1 200 ok
    [date] => sat, 29 may 2004 12:28:14 gmt
    [server] => apache/1.3.27 (unix)  (red-hat/linux)
    [last-modified] => wed, 08 jan 2003 23:11:55 gmt
    [etag] => 3f80f-1b6-3e1cb03b
    [accept-ranges] => bytes
    [content-length] => 438
    [connection] => close
    [content-type] => text/html
)

 代码如下 复制代码
//判断远程文件是否存在  
function remote_file_exists($url) {  
        $executetime = ini_get('max_execution_time');  
        ini_set('max_execution_time', 0);  
        $headers = @get_headers($url);  
        ini_set('max_execution_time', $executetime);  
        if ($headers) {  
            $head = explode(' ', $headers[0]);  
            if ( !emptyempty($head[1]) && intval($head[1])         }  
        return false;  
 }
例2
排除重定向的例子:
代码如下 复制代码
/**
 * fetches all the real headers sent by the server in response to a http request without redirects
 * 获取不包含重定向的报头
 */
function get_real_headers($url,$format=0,$follow_redirect=0) {
  if (!$follow_redirect) {
    //set new default options
    $opts = array('http' =>
        array('max_redirects'=>1,'ignore_errors'=>1)
    );
    stream_context_get_default($opts);
  }
  //get headers
    $headers=get_headers($url,$format);
    //restore default options
  if (isset($opts)) {
    $opts = array('http' =>
        array('max_redirects'=>20,'ignore_errors'=>0)
    );
    stream_context_get_default($opts);
  }
  //return
    return $headers;
}
http://www.bkjia.com/phpjc/445281.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/445281.htmltecharticle以前我有讲过程关于php判断远程文件是否存在的文章,那里都介绍利用fopen,sockt,curl函数来实现检查远程文件是否存在了,下面我再介绍利用...
其它类似信息

推荐信息