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

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP使用get_headers函数判断远程文件是否存在的方法。分享给大家供大家参考。具体实现方法如下:

以前讲过程关于php判断远程文件是否存在的文章都是利用fopen,sockt,curl函数来实现检查远程文件是否存在,下面我再介绍利用 get_headers来检查远程文件是否存在,感兴趣的朋友可以参考一下。

先来简单了解get_headers()函数

get_headers() 返回一个数组m包含有服务器响应一个 HTTP 请求所发送的标头。

get_headers:发送服务器响应HTTP请求

get_headers(字符串url[链接格式])

get_headers()以数组的形式返回服务器HTTP请求m如果执行失败,将返回FALSE和一个错误的水平E_WARNING,

可选参数设置为1,get_headers()能分析系统的响应速度和集数组中的键,

注意:使用该函数需要把 php.ini里面的allow_url_fopen = On,才能使用

实例代码如下:

复制代码 代码如下:
<?php
$url = '//www.jb51.net';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>

运行结果如下:
复制代码 代码如下:
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Cache-Control: max-age=1800
    [2] => Content-Length: 54874
    [3] => Content-Type: text/html
    [4] => Content-Location: //www.jb51.net/index.htm
    [5] => Last-Modified: Fri, 28 Nov 2014 03:34:56 GMT
    [6] => Accept-Ranges: bytes
    [7] => ETag: "b66ba847bcad01:bc5"
    [8] => Server: Microsoft-IIS/6.0
    [9] => Date: Fri, 28 Nov 2014 03:37:34 GMT
    [10] => Connection: close
)
Array
(
    [0] => HTTP/1.1 200 OK
    [Cache-Control] => max-age=1800
    [Content-Length] => 54874
    [Content-Type] => text/html
    [Content-Location] => //www.jb51.net/index.htm
    [Last-Modified] => Fri, 28 Nov 2014 03:34:56 GMT
    [Accept-Ranges] => bytes
    [ETag] => "b66ba847bcad01:bc5"
    [Server] => Microsoft-IIS/6.0
    [Date] => Fri, 28 Nov 2014 03:37:35 GMT
    [Connection] => close
)

判断远程文件是否存在代码如下:
复制代码 代码如下:
//判断远程文件是否存在  
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;  
}

排除重定向的实例代码如下:
复制代码 代码如下:
<?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; 

?>

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

PHP生成腾讯云COS接口需要的请求签名

PHP生成腾讯云COS接口需要的请求签名

COS和请求签名是什么 COS 是腾讯云对象存储的缩写及简称,请求签名是第三方在调用COS相关接口时需要按需提供的、经过特定算法创建而成的一组字符串信息,将唯一的标识当前第三方身份,提供...

两款万能的php分页类

本文为大家分享个超级好用、万能的php分页类,具体的实现代码如下 第一款php分页类 <?php /* * To change this template, choo...

PHP实现更改hosts文件的方法示例

本文实例讲述了PHP实现更改hosts文件的方法。分享给大家供大家参考,具体如下: 有这样一个需求,我有多个网址希望在不同的时候对应不同的 ip,如果一个个配 hosts,这工作显得有些...

php传值赋值和传地址赋值用法实例分析

本文实例讲述了php传值赋值和传地址赋值用法。分享给大家供大家参考。具体如下: <?php $name = 'Simon'; //对变量$name进行赋值(传值赋值)...

php的数组与字符串的转换函数整理汇总

1.将一个字符串转化为数组str_split()用于将一个字符串转化为数组语法:复制代码 代码如下:str_split(string,length)<SPAN style="COL...