PHP checks whether remote files exist & lt ;? Php $ url = 'http: // example.com '; $ hander_array = get_headers ($ url); if ($ header_array [0] = 'http/1.20. OK ') PHP checks whether remote files exist
? Briefly explain the above code. Get_headers is used to access a remote address and return the HTTP header sent by the server as an array. $ Header [0] indicates the status code returned by the server (if no exception occurs, the status code should be the first one ).
To determine whether a file exists on the remote server, you only need to confirm that the status code returned when accessing this file is "HTTP/1.1 200 OK" (of course, you can also determine that if the status code is Not "HTTP/1.1 404 Not Found ", file exists, but I always feel insecure. after all, there are other status codes such as 301,400 ).
Example of getting three HTTP response codes:
? Example of how to exclude redirection:
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 $ headers;}?>
?