PHP Check File Exists: Using file_exists() Function

To check if a file exists in PHP, you can use the file system function file_exists(). This function is used to determine whether a specified file or directory exists and returns a boolean value.

Here is an example using the file_exists() function to check if a file exists:

$filename = 'path/to/file.txt';

if (file_exists($filename)) {
    echo "文件存在";
} else {
    echo "文件不存在";
}

In the above example, we store the file path in the variable $filename and then use the file_exists() function to check if the file exists. If the file exists, it will output “File exists”, otherwise it will output “File does not exist”.

bannerAds