Home > Backend Development > PHP Tutorial > A piece of php code to detect whether the remote file exists

A piece of php code to detect whether the remote file exists

WBOY
Release: 2016-07-25 09:00:49
Original
795 people have browsed it
  1. /**

  2. php detects whether the remote file exists
  3. by http://bbs.it-home.org
  4. */
  5. function get_http_response_code($theURL) {
  6. $headers = get_headers($theURL);
  7. return substr($headers[0] , 9, 3);
  8. }

  9. /**

  10. * Fetches all the real headers sent by the server in response to a HTTP request without redirects
  11. * Gets the headers without redirects
  12. */
  13. function get_real_headers($url,$format=0,$follow_redirect=0) {
  14. if (!$follow_redirect ) {
  15. //set new default options
  16. $opts = array('http' =>
  17. array('max_redirects'=>1,'ignore_errors'=>1)
  18. );
  19. stream_context_get_default($opts);
  20. }
  21. //get headers
  22. $headers=get_headers($url,$format);
  23. //restore default options
  24. if (isset($opts)) {
  25. $opts = array('http' =>
  26. array ('max_redirects'=>20,'ignore_errors'=>0)
  27. );
  28. stream_context_get_default($opts);
  29. }
  30. //return
  31. return $headers;
  32. }
  33. ?>

Copy code
For curl judgment method, please refer to: php code for using curl to judge whether a remote file exists .


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template