PHP offers many functions for file manipulation. The file_exists
function checks if a file or directory exists at the specified path.
The syntax for file_exists
function is shown below:
file_exists(string $path): bool
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.
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 following code shows how we can use the file_exists
function:
<?phpif (file_exists("test.txt")) {echo "File exists.\n";}?>