PHP checks whether remote files exist

Source: Internet
Author: User
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 ). Make sure that a file is in.. SyntaxHighlighter. all ();

 

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:

Function get_http_response_code ($ theURL ){

$ Headers = get_headers ($ theURL );

Return substr ($ headers [0], 9, 3 );

}

?>

Example of how to exclude redirection:

 

/**

* Fetches all the real headers sent by the server in response to a HTTP request without redirects

* Obtain a header that does not contain redirection.

*/

 

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;

}

?>

 

This article fixed link: http://www.xssxss.com/fuck/323.xss | Shine's holy heaven-Min Chen 〃

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.