The file_exists() function checks whether or not a file or directory exists.
PHP file_exists() Function has the following syntax.
file_exists(path)
Parameter | Is Required | Description |
---|---|---|
path | Required. | Path to check |
Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
file_exists() returns true if the file exists and false otherwise.
We can use file_exists() to discover whether a file exists before attempting to open it:
<?PHP//from ww w .ja v a 2s . c o m
if (file_exists("data.txt")) {
print "data.txt exists!\n";
} else {
print "data.txt does not exist!\n";
}
echo file_exists("data.txt");
?>
The code above generates the following result.