Use phpget_headers to determine whether the URL is valid. To determine whether a file or directory exists in php, we usually think of two functions: is_file and file_exists. However, these two functions determine whether a remote url file exists in php and whether a file or directory exists. generally, we will think of the is_file and file_exists functions. However, the two functions determine whether a remote url file exists. The author will share with you a way to use the php get_headers function to determine whether a remote url file exists effectively.
For the functions and usage of the php get_headers function, refer to this article:
The role and usage of the get_headers function in php
The following describes how to use php get_headers to determine the true validity of a url.
Through the introduction of this function, we can know that this function simply returns the header file information of an HTTP request. The information format is basically as follows:
(1)
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
(2)
Array
(
[0] => HTTP/1.0 404 Not Found
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
From the above two cases, we can easily see that, if you determine whether the url is valid, it must be determined by the first element value in the array. If the server returns 200, that is, the file is returned correctly. if the server returns 404, that is, the file does not exist, you can easily determine whether a url exists.
(Detailed Source: PHP programmer notes)
Bytes. However, the two functions determine whether a remote url file exists...