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
clearstatcachefunction 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.
Code
The following code shows how we can use the file_exists function:
main.php
test.txt
<?phpif (file_exists("test.txt")) {echo "File exists.\n";}?>
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved