Example 1
| The code is as follows: |
Copy code |
|
// Determine the remote file
Function check_remote_file_exists ($ url)
{
$ Curl = curl_init ($ url );
// Do not retrieve data
Curl_setopt ($ curl, CURLOPT_NOBODY, true );
// Send the request
$ Result = curl_exec ($ curl );
$ Found = false;
// Failed to send the request
If ($ result! = False ){
// Check whether the http response code is 200.
$ StatusCode = curl_getinfo ($ curl, CURLINFO_HTTP_CODE );
If ($ statusCode = 200 ){
$ Found = true;
}
}
Curl_close ($ curl );
Return $ found;
} |
Of course, there are also many other methods with more or less restrictions and defects, such:
(1) use the fopen () function. It must be enabled in allow_url_open state. Otherwise, an error is returned.
| The code is as follows: |
Copy code |
$ Url = 'http: // www.111cn.net/img/qrcode_for_phpddt.JPG ';
If (@ fopen ($ url, 'r ')){
Echo 'file exist ';
} Else {
Echo 'file does not exist ';
} |
(2) get_headers gets all the headers sent by the server in response to an HTTP request, which is less efficient. You can test it.
| The code is as follows: |
Copy code |
|
$ Url = 'http: // www.111cn.net/img/qrcode_for_phpddt.JPG ';
Stream_context_set_default (
Array (
'Http' => array (
'Timeout' => 1,
)
)
);
$ Headers = get_headers ($ url );
If (preg_match ('/ 200/', $ headers [0]) {
Echo 'file exist ';
} Else {
Echo 'file does not exist ';
} |
(3) file_get_contents () function
| The code is as follows: |
Copy code |
$ Opts = array (
'Http' => array (
'Timeout' => 3,
)
);
$ Context = stream_context_create ($ opts );
$ Resource = @ file_get_contents ('http: // www.111cn.net/img/qrcode_for_phpddt.JPG ', false, $ context );
If ($ resource ){
Echo 'file exist ';
} Else {
Echo 'file does not exist ';
} |
Reference: fopen function usage
| Parameters |
Description |
| Filename |
Required. Specifies the file or URL to open. |
| Mode |
Required. Specifies the access type of the file/stream. Possible values are shown in the table below. |
| Include_path |
Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE. |
| Context |
Optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream. |
Possible value of the mode parameter
| Mode |
Description |
| "R" |
Open the file in read-only mode and point the file pointer to the file header. |
| "R +" |
Open in read/write mode and point the file pointer to the file header. |
| "W" |
Open in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
| "W +" |
Open in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
| "" |
Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
| "A +" |
Open in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
| "X" |
Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.
This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) system call.
This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
| "X +" |
Create and open the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.
This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) system call.
This option is supported by PHP 4.3.2 and later versions and can only be used for local files |