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";
}
?>
Copyright ©2024 Educative, Inc. All rights reserved