HOWTO · PHP
How to Check if File Exists in PHP
This article explains how to check if a file exists in PHP using the file_exists() function. Learn to handle file and directory checks effectively with practical examples. Enhance your PHP applications by ensuring file existence before operations and avoid common errors.
On this page
Use PHP’s file_exists() to check whether a path currently resolves to an existing file or directory. It returns a Boolean, so you can branch on the result before deciding what to do. The check describes the path at that moment; it does not guarantee that a later file operation will succeed.
Check whether a path exists with file_exists()
Pass the path as a string. This self-contained example creates a temporary file, checks it, and removes it afterward, so the success branch does not depend on a pre-existing project file.
Run this example:
$directory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'file-exists-' . uniqid('', true);
mkdir($directory);
$file = $directory . DIRECTORY_SEPARATOR . 'example.txt';
file_put_contents($file, 'Ready');
if (file_exists($file)) {
echo "File is ready.\n";
} else {
echo "File was not found.\n";
}
unlink($file);
rmdir($directory);
This example is provided without an output claim because this packet contains no execution evidence. In your own code, use the same conditional pattern with the path you need to check. A true result means the path exists; false means PHP did not find it at that check.
Distinguish files from directories
file_exists() also returns true for a directory. If the next step requires a directory specifically, test it with is_dir(); for a regular file, use is_file(). These checks answer different questions: existence alone does not identify the path’s type.
$path = __DIR__ . DIRECTORY_SEPARATOR . 'uploads';
if (is_dir($path)) {
echo 'The path is a directory.';
} elseif (is_file($path)) {
echo 'The path is a file.';
} else {
echo 'No file or directory was found at this path.';
}
The path is built from __DIR__, which refers to the directory containing the current PHP file. Change the final path component to the location your application needs to inspect.
Handle paths and check results safely
A relative path is interpreted in the context of the running PHP process, so the same string can identify different locations when the working directory differs. An absolute path starts from a filesystem root and makes the intended location explicit. When a relative path surprises you, inspect the path PHP is using—for example, combine it with getcwd() while diagnosing the issue—or construct it from a known directory such as __DIR__.
Use the check to choose a sensible next step, but handle the operation itself as fallible. A path can change after the check, and existence does not establish that the process can read, write, or otherwise use it. Check the result of the actual operation too, and handle its failure according to the application.
Common limitations and troubleshooting
If file_exists() returns false unexpectedly, verify the spelling and construction of the path, then confirm that it points to the intended location in the PHP process’s environment. Also consider whether the path changed between checks or whether the running process can inspect the relevant filesystem location. The function reports its path-existence check; it does not diagnose the cause of a false result.
The PHP manual describes file_exists() as checking whether a file or directory exists and notes that its result is subject to the configured filesystem context. Avoid treating it as a remote-resource availability check: use the protocol or service-specific mechanism appropriate to the resource instead. See the PHP manual for file_exists() for the function’s parameter, return value, and notes.
For repeated checks, avoid assuming that a prior result remains current. Perform the operation you need and handle its result directly when possible. This keeps the program correct if the filesystem changes between the check and the operation.