PHP使用get_headers函数判断远程文件是否存在的方法
2015-01-24信息快讯网
这篇文章主要介绍了PHP使用get_headers函数判断远程文件是否存在的方法,以实例形式分析了使用get_headers函数对远程文件是否存在进行判断的方法,以及针对重定向的排除方法,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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 = 'http://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: http://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] => http://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程序设计有所帮助。
ucenter通信原理分析
php计划任务之ignore_user_abort函数实现方法
WampServer下安装多个版本的PHP、mysql、apache图文教程
PHP PDO fetch 模式各种参数的输出结果一览
PHP和Shell实现检查SAMBA与NFS Server是否存在
php实现utf-8转unicode函数分享
windows中为php安装mongodb与memcache
phpQuery让php处理html代码像jQuery一样方便
php中Socket创建与监听实现方法
php下Memcached入门实例解析
ioncube_loader_win_5.2.dll的错误解决方法
php中socket通信机制实例详解
php从memcache读取数据再批量写入mysql的方法
PHP中使用xmlreader读取xml数据示例
dedecms集成财付通支付接口
为PHP5.4开启Zend OPCode缓存
php进行支付宝开发中return_url和notify_url的区别分析
腾讯微博提示missing parameter errorcode 102 错误的解决方法
PHP实现将HTML5中Canvas图像保存到服务器的方法
php的mssql数据库连接类实例
php中fgetcsv()函数用法实例
Yii入门教程之Yii安装及hello world
浅析php适配器模式(Adapter)
PHP魔术方法__GET、__SET使用实例
使用PHP Socket 编程模拟Http post和get请求
PHP中substr()与explode()函数用法分析
thinkphp使用phpmailer发送邮件的方法
php中base64_decode与base64_encode加密解密函数实例
php中convert_uuencode()与convert_uuencode函数用法实例
thinkphp使用literal防止模板标签被解析的方法
php中HTTP_REFERER函数用法实例
PHP通过内置函数memory_get_usage()获取内存使用情况
php中ob_get_length缓冲与获取缓冲长度实例
PHP图像处理之imagecreate、imagedestroy函数介绍
php管理nginx虚拟主机shell脚本实例
php集成环境xampp中apache无法启动问题解决方案
php中addslashes函数与sql防注入
php中explode函数用法分析
php调用shell的方法