php专区

 首页 > php专区 > PHP应用 > php函数大全 > PHP get_headers函数判断远程文件是否存在

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

分享到:
【字体:
导读:
          以前我有讲过程关于php判断远程文件是否存在的文章,那里都介绍利用fopen,sockt,curl函数来实现检查远程文件是否存在了,下面我再介绍利用 get_headers来检查远程文件是否存在,有需要了解的朋友可参考。先来简

以前我有讲过程关于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]) < 400) return true;  
        }  
        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;
}  
分享到:
PHP preg_match正则表达式的使用
在php中preg_match()函数是用来执行正则表达式的一个常用的函数,下面我来给大家详细介绍preg_match使用方法。   函数用法 int preg_match_all ( string pattern, string subject, array matches [, int flags] ) 例1  代码如下 复制代码 preg_match_all ("|]+>(.*)]+>|U","example: this is a test ...
PHP截断函数mb_substr()详细介绍
在php中mb_substr()函数是用来截中文与英文的函数,可以方便快速的解决截取指定字符长度的问题,下面我来给大家介绍介绍。   提示:mb_substr在于php中是默认不被支持的我们需要在在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到;extension=php_mbstring.dll把前面的;号去掉才可以使用mb_substr函数哦。 F...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……