PHPget_headers function to determine whether a remote file exists _ PHP Tutorial

Source: Internet
Author: User
The PHPget_headers function checks whether a remote file exists. I used to talk about articles about how to determine whether a remote file exists in php, where I used the fopen, sockt, and curl functions to check whether the remote file exists, next I will introduce the article about how to use php to determine whether a remote file exists. I will introduce how to use the fopen, sockt, and curl functions to check whether a remote file exists, next I will introduce how to use get_headers to check whether the remote file exists. For more information, see.

First, let's take a look at the get_headers () function.

Get_headers () returns an array containing the header sent by the server to respond to an HTTP request.

Get_headers: Send the server response HTTP request
Get_headers (string url [link format])
Get_headers () returns the HTTP request of the server as an array. If the execution fails, FALSE and an error level E_WARNING are returned.

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

Note: To use this function, you must use allow_url_fopen = On in php. ini.

Example

The code is as follows:

$ Url = 'http: // www.example.com ';

Print_r (get_headers ($ url ));

Print_r (get_headers ($ url, 1 ));
?>

Return value

Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)

Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] = & gt; 438
[Connection] => close
[Content-Type] => text/html
)


Example

The code is as follows:

// 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]) <400) return true;
}
Return false;
}

Example 2

Example of how to exclude redirection:

The code is as follows:
/**
* Fetches all the real headers sent by the server in response to a HTTP request without redirects
* Obtain a header that does not contain redirection.
*/

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;
}

...

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.