PHP uses the Get_headers function to determine if a remote file exists _php

Source: Internet
Author: User
Keywords Php get_headers function judgment remote
Tags ranges
This example describes how PHP uses the Get_headers function to determine whether a remote file exists. Share to everyone for your reference. The implementation method is as follows:

Before the process about PHP to determine whether remote files exist in the article is the use of Fopen,sockt,curl function to check whether remote files exist, the following I introduce the use of get_headers to check whether remote files exist, interested friends can refer to.

Let's take a quick look at the get_headers () function

Get_headers () returns an array m containing the headers sent by the server in response to an HTTP request.

Get_headers: The sending server responds to HTTP requests

Get_headers (string url[link format])

Get_headers () returns the server HTTP request as an array m if execution fails, it will return false and an error level e_warning,

The optional parameter is set to 1,get_headers () to analyze the system's response speed and the keys in the set array.

The code is as follows:

<?php
$url = ' http://www.bitsCN.com ';
Print_r (Get_headers ($url));
Print_r (Get_headers ($url, 1));
?>


The results of the operation are as follows:

The code is as follows:

Array
(
[0] = = http/1.1-OK
[1] = cache-control:max-age=1800
[2] = content-length:54874
[3] = content-type:text/html
[4] = content-location:http://www.bitscn.com/index.htm
[5] = Last-modified:fri, 03:34:56 GMT
[6] = Accept-ranges:bytes
[7] = ETag: "B66ba847bcad01:bc5"
[8] = server:microsoft-iis/6.0
[9] = Date:fri, 03:37:34 GMT
[Ten] = Connection:close
)
Array
(
[0] = = http/1.1-OK
[Cache-control] = max-age=1800
[Content-length] = 54874
[Content-type] = text/html
[Content-location] = http://www.bitsCN.com/index.htm
[Last-modified] = Fri, 03:34:56 GMT
[Accept-ranges] = bytes
[ETag] = "B66ba847bcad01:bc5"
[Server] = microsoft-iis/6.0
[Date] = Fri, 03:37:35 GMT
[Connection] = Close
)


Determine if the remote file has the following code:

The code is as follows:

Determine if a remote file exists
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]) < +) return true;
}
return false;
}


The instance code for excluding redirects is as follows:

The code is as follows:

<?php
/**
* Fetches all the real headers sent by the server in response to a HTTP request without redirects
* Get headers that do not contain 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;
}
?>

I hope this article is helpful to everyone's PHP programming.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.