Example of whether PHP detects the existence of a remote file

Source: Internet
Author: User
Tags response code
  1. $url = ' http://xxxx.com ';
  2. $hander _array = get_headers ($url);
  3. if ($header _array [0] = = ' http/1.1 OK ') {
  4. echo ' file exists ';
  5. } else {
  6. Echo ' file does not exist ';
  7. }
Copy Code

Description: Get_headers accesses a remote address and returns the HTTP headers sent by the server as an array. The $header[0] is the status code returned by the server (the status code should be the first to return if not unexpectedly).

To determine if a file exists on the remote server, just make sure that the status code returned by accessing the file is "http/1.1" (Status code http/1.1 404 Not Found can detect a file does not exist? Readers).

For an example of a three-bit HTTP response code, refer to a section of PHP code that detects the existence of a remote file.

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