Home > Backend Development > PHP Tutorial > Example of php detecting whether a remote file exists

Example of php detecting whether a remote file exists

WBOY
Release: 2016-07-25 09:00:45
Original
820 people have browsed it
  1. $url = 'http://xxxx.com';
  2. $hander_array = get_headers ( $url );
  3. if ($header_array [0] == 'HTTP/1.1 200 OK' ) {
  4. echo 'The file exists';
  5. } else {
  6. echo 'The file does not exist';
  7. }
Copy code

Instructions: get_headers accesses a remote address and returns 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).

To confirm that a file exists on the remote server, just make sure that the status code returned by accessing the file is "HTTP/1.1 200 OK" (Can the status code HTTP/1.1 404 Not Found detect that a file does not exist? Leave it to the readers to think).

For an example of obtaining a three-digit HTTP response code, please refer to the article PHP code to detect 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