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

Source: Internet
Author: User
Tags ranges

This example describes the way PHP uses the Get_headers function to determine whether a remote file exists. Share to everyone for your reference. The implementation methods are as follows:

Before speaking about the process of PHP to determine whether the existence of remote files are the use of Fopen,sockt,curl function to check the existence of remote files, the following I introduce the use of get_headers to check whether the remote file exists, interested friends can refer to.

First to understand the get_headers () function

Get_headers () returns an array of m containing headers that are 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 m as an array if execution fails, it returns false and an incorrect horizontal e_warning.

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

Note: Using this function you need to put the Allow_url_fopen = on in the php.ini to use

The instance code is as follows:

Copy Code code as follows:
<?php
$url = ' http://www.jb51.net ';
Print_r (Get_headers ($url));
Print_r (Get_headers ($url, 1));
?>

The results of the operation are as follows:
Copy Code code 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.jb51.net/index.htm
[5] => Last-modified:fri, Nov 2014 03:34:56 GMT
[6] => Accept-ranges:bytes
[7] => ETag: "B66ba847bcad01:bc5"
[8] => server:microsoft-iis/6.0
[9] => Date:fri, Nov 2014 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.jb51.net/index.htm
[Last-modified] => Fri, Nov 2014 03:34:56 GMT
[Accept-ranges] => bytes
[ETag] => "B66ba847bcad01:bc5"
[Server] => microsoft-iis/6.0
[Date] => Fri Nov 2014 03:37:35 GMT
[Connection] => Close
)

Determine if the remote file exists code as follows:
Copy Code code as follows:
To determine whether 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:
Copy Code code as follows:
<?php
/**
* Fetches all of the real headers sent from 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 will help you with your PHP program design.

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.