PHP file_exists() Function

What is PHP file_exists() function?

file_exists() function can tell you whether a file or directory exists in a specified path.

Syntax:

file_exists(path_to_file_or_directory)

Parameters:

The function has only one parameter-

path_to_file_or_directory: It is the path to the file or directory that you want to check. It must be a string.

Return Values:

Usually, the function returns –

  • TRUE, if the file or directory exists in the specified path,
  • FALSE, if the file or directory doesn’t exist in the specified path,

However, take a look at what the function returns in the following scenarios-

  • Unexpected true or false, If the file is larger than 2GB and the platform is 32bit integers.
  • FALSE, if the parameter is an empty string.
  • FALSE, if you try to access a file or directory of a remote URL.

Examples:

Example 1:

<?php
if(file_exists("docs/notes.docx")){
    echo "File Exists.";
}else{
    echo "File doesn't Exist.";
}
?>

Explanation:

If the file notes.docx is located in docs folder, file_exists() function will return TRUE and the program will display “File Exists.”. On the other hand, if the file doesn’t exist, the function will return FALSE and the program will display “File doesn’t Exist.”

Best Practices:

Though, the return value is cached for faster performance for the later usages, but, for the following cases, you’ll clear the cache-

  • If the file is being cached multiple times in the same script and the file is in risk of being removed.
  • Or, if the file is being cached multiple times in the same script and it could be changed during the operation.

In both cases, you’ll get wrong file information. To clear the status cache, use clearstatcache() function.

Practical Usages of PHP file_exist() Function:

Few scenarios when you’ll need file_exist() function could be-

  • You may want to process information to an existing file or just want to read content of the file. In this case, before writing information in the file, you’ll use file_exist() function to check whether the file exist.
  • You may need to upload files but you don’t want to overwrite with the existing files. In this case, you’ll need to use this file_exist() function.
  • You may want to move a file from one directory to another one.

Caution:

PHP cache the result of the file_exists() function. So if you call this function multiple times in a script PHP sends you cached result. The problem is if you change the remove the file, you may not get the up-to-date result of the file. So, to get the latest result, clear the cache with clearstatcache() function. Check the code below-

<?php
$filename = "file/doc.txt";
var_dump(file_exists($filename));
// Some codes.
var_dump(file_exists($filename));
// Some codes.
clearstatcache();
var_dump(file_exists($filename));
// Some codes.
?>

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP file_exists() Function

You’ll frequently use file_exists() function when you work with files. It is a built-in useful filesystem function in PHP that can confirm you a file’s existence.

Reference:

https://www.php.net/manual/en/function.file-exists.php