Home  >  Article  >  Backend Development  >  How to detect the existence of remote files in php

How to detect the existence of remote files in php

WBOY
WBOYOriginal
2016-07-25 09:07:281205browse
  1. function get_http_response_code($theURL) {
  2. $headers = get_headers($theURL);
  3. return substr($headers[0], 9, 3);
  4. }
  5. ?>
Copy code

The function of get_headers is to access a remote address and return the HTTP headers sent by the server in the form of an array. And $header[0] is the status code returned by the server (if nothing else happens, the status code should be the first one returned).

Exclusion example:

  1. /**

  2. * Fetches all the real headers sent by the server in response to a HTTP request without redirects
  3. * Gets the headers without redirects
  4. */
  5. function get_real_headers($url,$format=0,$follow_redirect=0) {
  6. if (!$follow_redirect) {
  7. //set new default options
  8. $opts = array('http' =>
  9. array('max_redirects'=>1,'ignore_errors'=>1)
  10. );
  11. stream_context_get_default($opts);
  12. }

  13. //get headers

  14. $headers=get_headers($url,$format);
  15. //restore default options
  16. if (isset($opts)) {
  17. $opts = array(' http' =>
  18. array('max_redirects'=>20,'ignore_errors'=>0)
  19. );

  20. stream_context_get_default($opts);

  21. }

  22. //return

  23. return $headers;
  24. }
  25. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn