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
PHP Advanced
PHP File Upload
PHP File Handling
PHP File Inclusion
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is include() function in PHP?
Can we include a file two times in any PHP page?
Which is the PHP function to delete a file from server?
What is fopen function PHP?
What is the difference between unlink() and unset() in PHP?
How to check if a file exists in PHP?
How to get the size of a file in PHP?