PHP uses the get_headers function to determine whether a remote file exists ,. PHP uses the get_headers function to determine whether a remote file exists. this example describes how PHP uses the get_headers function to determine whether a remote file exists. Let's share with you how PHP uses the get_headers function to determine whether a remote file exists,
This example describes how PHP uses the get_headers function to determine whether a remote file exists. Share it with you for your reference. The specific implementation method is as follows:
In the previous articles about php's determination of the existence of remote files, fopen, sockt, and curl functions were used to check the existence of remote files, next I will introduce how to use get_headers to check whether the remote file exists. if you are interested, refer to it.
First, let's take a look at the get_headers () function.
Get_headers () returns an array. m contains 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 server HTTP request m in 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.
The instance code is as follows:
The code is as follows:
<? Php
$ Url = 'http: // www.bkjia.com ';
Print_r (get_headers ($ url ));
Print_r (get_headers ($ url, 1 ));
?>
The running result is as follows:
The code is as follows:
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.bkjia.com/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] = & gt; 54874
[Content-Type] => text/html
[Content-Location] => http://www.bkjia.com/index.htm
[Last-Modified] => Fri, 28 Nov 2014 03:34:56 GMT
[Accept-Ranges] => bytes
[ETag] => "b66ba847bcad01: bc5"
[Server] = & gt; Microsoft-IIS/6.0
[Date] => Fri, 28 Nov 2014 03:37:35 GMT
[Connection] => close
)
The code used to determine whether a remote file exists is as follows:
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;
}
The code for excluding redirection instances 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
* 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;
}
?>
I hope this article will help you with PHP programming.
Example: This article describes how PHP uses the get_headers function to determine whether a remote file exists. Share it with you...