What is file_exists in PHP?

PHP offers many functions for file manipulation. The file_exists function checks if a file or directory exists at the specified path.

Syntax

The syntax for file_exists function is shown below:

file_exists(string $path): bool

Parameters

The file_exists function takes only one parameter:

  • path: This is a required parameter. This is a string that specifies the path to the file or directory to check.

The results of this function are stored in the cache. You can use the clearstatcache function to clear the cache.

Return value

The function returns a bool value, true if the file or directory exists at the specified path, and returns false if it does not.

The working of file_exists function

Code

The following code shows how we can use the file_exists function:

main.php
test.txt
<?php
if (file_exists("test.txt")) {
echo "File exists.\n";
}
?>
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved