How to check if a file exists in PHP?
Use the file_exists() function. It takes the file name or path and returns true if it exists, otherwise false.
<?php
$file = "data.txt";
if (file_exists($file)) {
echo "File found";
} else {
echo "File not found";
}
?>
Output:
If data.txt is not in the folder
File not found
Note that file_exists() also returns true for folders. Use is_file() to check only files and is_dir() to check only folders.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |