Php method for detecting the existence of remote files

Source: Internet
Author: User
Php method for detecting the existence of remote files

  1. Function get_http_response_code ($ theURL ){
  2. $ Headers = get_headers ($ theURL );
  3. Return substr ($ headers [0], 9, 3 );
  4. }
  5. ?>

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 ).

Example of how to exclude redirection:

  1. /**

  2. * Fetches all the real headers sent by the server in response to a HTTP request without redirects
  3. * Obtain a header that does not contain redirection.
  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. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.