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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
