PHP 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,才能使用

 代码如下 复制代码

<?php
$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

排除重定向的例子:
  

 代码如下 复制代码
<?php
/**
 * 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;
}  
时间: 2024-08-01 02:00:09

PHP get_headers函数判断远程文件是否存在的相关文章

PHP使用get_headers函数判断远程文件是否存在的方法_php技巧

本文实例讲述了PHP使用get_headers函数判断远程文件是否存在的方法.分享给大家供大家参考.具体实现方法如下: 以前讲过程关于php判断远程文件是否存在的文章都是利用fopen,sockt,curl函数来实现检查远程文件是否存在,下面我再介绍利用 get_headers来检查远程文件是否存在,感兴趣的朋友可以参考一下. 先来简单了解get_headers()函数 get_headers() 返回一个数组m包含有服务器响应一个 HTTP 请求所发送的标头. get_headers:发送服务

判断远程文件是否存在的php函数

函数|是否存在 判断远程的文件是否存在. <?php /*  函数:remote_file_exists  功能:判断远程文件是否存在  参数: $url_file - 远程文件URL  返回:存在返回true,不存在或者其他原因返回false*/function remote_file_exists($url_file){ //检测输入 $url_file = trim($url_file); if (empty($url_file)) { return false; } $url_arr =

php下利用curl判断远程文件是否存在的实现代码_php技巧

复制代码 代码如下: //判断远程文件 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 $

php提前判断远程文件是否可用

 代码如下 复制代码 //判断远程文件 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 $

PHP利用curl判断远程文件是否存在的方法

  PHP利用curl判断远程文件是否存在,请看下边的代码: //判断远程文件 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) { /

C# 判断远程文件是否存在

#region 判断远程文件是否存在 /// <summary> /// 判断远程文件是否存在 /// </summary> /// <param name="fileUrl"></param> /// <returns></returns> public static bool RemoteFileExists(string fileUrl) { HttpWebRequest re = null; HttpWeb

PHP中通过fopen()函数访问远程文件示例_php技巧

使用PHP不仅可以让用户通过浏览器访问服务器端的文件,还可以通过HTTP或FTP等协议访问其他服务器中的文件,可以在大多数需要用文件名作为参数的函数中使用HTTP和FTP URL来代替文件名.使用fopen()函数将指定的文件名与资源绑定到一个流上,如果文件名是"scheme://-"的格式,则被当成一个URL,PHP将搜索协议处理器(也被成为封装协议)来处理此模式. 如果需要远程访问文件,必须在PHP的配置文件中激活"allow_url_fopen"选项,才能使用

[求助]问一个关于远程文件确定的菜鸟问题,怎么判断远程文件和文件夹??

问题描述 我用一个方法得到了FTP上的目录及文件,(用的是WebRequestMethods.Ftp等实现),填充到了一个字符数组中(string[]list)现在,我有一个问题,如果想判断list数组中的每一项是文件还是文件夹呢?请大侠们指点江山. 解决方案 解决方案二:我用过一个办法,就是一般文件都有括展名,所以文件都包含一个字符".",文件夹一般不包含,但是这种方法毕竟不是好的方法,谁还有方法?解决方案三:精神可佳,鼓励一下自己.解决方案四:你得到的时候,能判断文件和文件夹?如果

判断远程文件是否存在

        private bool RemoteFileExists(string fileUrl)        {            try            {                HttpWebRequest re = (HttpWebRequest)WebRequest.Create(fileUrl);                HttpWebResponse res = (HttpWebResponse)re.GetResponse();