The simplest method is to use fopen () to check whether the file can be opened. If the file can be opened, the file will certainly exist.
Copy codeThe Code is as follows:
<? Php
$ Url = 'HTTP: // www.jb51.net/images/test.jpg ';
If (@ fopen ($ url, 'R '))
{
Echo 'file exists ';
}
Else
{
Echo 'file Do Not Exits ';
}
?>
Syntax: fopen (filename, mode, include_path, context)
| 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 |