W3docs

file_exists()

The file_exists() function is a built-in PHP function that checks if a file or directory exists. This function returns TRUE if the file or directory exists, and

What is the file_exists() Function?

The file_exists() function is a built-in PHP function that reports whether a given path points to either a file or a directory on the filesystem. It is one of the most common ways to guard file operations — you call it before reading, writing, or deleting so your script doesn't crash on a missing path.

It returns:

  • true if the path exists (whether it's a regular file, a directory, a symlink, or any other special file), or
  • false if nothing exists at that path, or if you don't have permission to read the information about it.

Syntax

file_exists(string $filename): bool

$filename is the path to check. It can be:

  • a relative path ('data/users.csv'), resolved against the current working directory, or
  • an absolute path ('/var/www/uploads/logo.png' or 'C:\\temp\\file.txt' on Windows).

A Basic Example

php— editable, runs on the server

If myfile.txt is present in the script's working directory, the first branch runs; otherwise the else branch runs. Because file_exists() returns a plain boolean, you can use it directly in any if, &&, or ternary expression.

Watch Out for the Stat Cache

PHP caches the result of filesystem checks (including file_exists()) to avoid hitting the disk repeatedly. Within a single script run, if you check a path, then create or delete it, then check again, you may still see the old answer.

Call clearstatcache() to force a fresh lookup:

<?php

$path = 'temp.txt';

file_put_contents($path, 'hello');      // create the file
clearstatcache();                       // forget the cached result
var_dump(file_exists($path));           // bool(true)

unlink($path);                          // delete the file
clearstatcache();
var_dump(file_exists($path));           // bool(false)

This caching matters in long-running scripts and loops that create or remove files on the fly.

file_exists() vs. is_file() vs. is_dir()

file_exists() cannot tell you what kind of thing exists — only that something does. When you need to know specifically, reach for a more precise function:

FunctionReturns true when the path is…
file_exists()a file or a directory (anything)
is_file()a regular file
is_dir()a directory
is_readable()exists and is readable by the current process
is_writable()exists and is writable by the current process

A common mistake is using file_exists() to confirm a path is a file before opening it. If a directory happens to share that name, file_exists() returns true but fopen() will then fail. Prefer is_file() for that case.

Common Use Cases

Guard a read so it never throws a warning:

<?php

$config = 'config.php';

if (is_file($config)) {
    require $config;
} else {
    die('Configuration file is missing.');
}

Avoid overwriting an existing upload by adding a numeric suffix:

<?php

$target = 'upload.png';
$i = 1;

while (file_exists($target)) {
    $target = "upload-$i.png";
    $i++;
}

echo "Saving to: $target";

Delete a file only if it's there, so unlink() doesn't warn about a missing path:

<?php

$tmp = 'cache.tmp';

if (file_exists($tmp)) {
    unlink($tmp);
}

Things to Keep in Mind

  • Race conditions. A file can be created or removed by another process between your file_exists() check and the operation that follows. For critical writes, it's often safer to attempt the operation and handle the error than to check first.
  • Permissions. If the directory leading to the file isn't accessible to the PHP process, file_exists() returns false even when the file is physically there.
  • Remote URLs. With the right configuration file_exists() can work on some stream wrappers, but it does not reliably check http:// URLs — use a function like file_get_contents() with error handling instead.

To go deeper on reading and writing files, see the PHP File Handling chapter.

Practice

Practice
What is the function of the 'file_exists()' in PHP?
What is the function of the 'file_exists()' in PHP?
Was this page helpful?