<?php
$url = ' http://example.com ';
$hander _array = get_headers ($url);
if ($header _array [0] = = ' http/1.1 OK ') {
echo ' file exists ';
} else {
Echo ' file does not exist ';
}
Simply explain the above code. The function of get_headers is to access a remote address and return the HTTP headers sent by the server as an array. The $header[0] is the status code returned by the server (the status code should be the first to return if not unexpectedly).
To determine the existence of a file on the remote server, just make sure that the status code returned by accessing the file is "http/1.1" (of course you can also tell if the status code is not "http/1.1 404 Not Found" Then the file exists, but the total feeling is not insured, There are, after all, other status codes such as 301,400.
Get an example of a three-bit HTTP response code:
<?php function Get_http_response_code ($theURL) { $headers = get_headers ($theURL); return substr ($headers [0], 9, 3); } ? >
Examples of excluding redirects:
<?php /** * Fetches all the real headers sent by the server in response to a HTTP request without redirects
* gets a header that does not contain a redirect */ 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; } ? >