Use file_exists() to check if a file is there. It returns true if the path exists, false otherwise. Simple.

<?php
if (file_exists('hello.txt')) {
    echo 'File exists';
} else {
    echo 'File not existing';
}

That’s the core. But I’ve wasted hours on edge cases here, so let’s cover the stuff that actually trips people up.


file_exists() is not just for files

Here’s the first gotcha: file_exists() returns true for directories too. If you have a folder named hello.txt, this check passes. That’s not what you want if you’re about to read it as a text file.

Use is_file() when you specifically mean a regular file. Use is_dir() when you mean a directory. They are more precise tools for the job.

<?php
if (is_file('hello.txt')) {
    // definitely a file, not a folder
}

if (is_dir('assets/')) {
    // definitely a folder
}

I used to mix these up in my early scripts. file_exists() is a broad net. is_file() and is_dir() are specific hooks. Pick the right one for what you’re checking.


Check permissions before you open

Knowing a file exists doesn’t mean you can touch it. You might have read-only permissions, or the file might be locked by another process.

is_readable() and is_writable() check those permissions. This is usually what you really need to know before calling fopen(). A file can exist and still fail to open because of permissions. Check the right thing first.

<?php
$path = 'config/settings.json';

if (file_exists($path) && is_readable($path)) {
    $content = file_get_contents($path);
}

Don’t skip this if your script runs as a different user than the one who created the files. It happens more often than you think in shared hosting setups.


Relative paths are relative to the working directory

This is the silent killer. If you use a relative path like 'hello.txt', PHP resolves it against the current working directory, not necessarily the folder where your script lives.

If you run php /var/www/app/index.php from /var/www/, the CWD is /var/www/. Your file might be in /var/www/app/. The check fails, and you don’t know why.

Use __DIR__ to anchor your paths to the script’s location. It’s explicit and predictable.

<?php
$basePath = __DIR__;
$file = $basePath . '/hello.txt';

if (file_exists($file)) {
    echo 'Found it relative to the script';
}

I know this feels like overkill for a single file. But the moment you have nested includes or CLI entry points, __DIR__ saves you from debugging “why is it looking in the wrong place?”


PHP caches these checks within a request

PHP remembers the result of file_exists() and similar stat calls during the current request. But it only caches the result for a file that does exist. If you check for a file, it exists, and then another process deletes it, file_exists() will keep returning true for the rest of the request.

This is the trap. You think the file is gone, but PHP is still holding onto the old “yes” answer.

Call clearstatcache() to force PHP to re-check the filesystem. You only need this if you’re deleting files (or if another process is) and checking for them in the same script execution. Note that PHP’s own unlink() clears the cache for you, so you only need this manual step when the deletion happens outside your script.

<?php
// Assume 'temp.txt' exists and we just checked it
if (file_exists('temp.txt')) {
    // ... some logic ...
}

// Another process deletes 'temp.txt' here

// Without clearstatcache(), this would still return true
if (file_exists('temp.txt')) {
    echo 'Still thinks it is there';
}

// Force a fresh check
clearstatcache();

if (file_exists('temp.txt')) {
    echo 'It is there now';
} else {
    echo 'Gone';
}

Don’t sprinkle clearstatcache() everywhere. It has a small performance cost. Only use it when you’ve modified the filesystem (or expect it to be modified) and need an accurate answer immediately after.


Don’t rely on file_exists() for race conditions

There’s a window between checking if a file exists and actually opening or creating it. In that window, another process can delete the file, or create the file you’re trying to make. This is a race condition.

For reading, just try to open it. If fopen() fails, handle the error. It’s simpler and more reliable than checking first.

For creating a file only if it doesn’t exist, use the 'x' mode in fopen(). It creates the file exclusively. If the file already exists, it fails. No check needed. See how fopen works for details on modes.

<?php
// Safe creation: fails if file already exists
$handle = fopen('lockfile.lock', 'x');
if ($handle === false) {
    echo "File already existed";
} else {
    fwrite($handle, 'locked at ' . time());
    fclose($handle);
}

This is the atomic way to do it. It avoids the gap where two scripts think they own the lock.


Use the right tool for the job

file_exists() is a starting point, not the whole story. Know that it includes directories. Check permissions with is_readable() and is_writable(). Anchor your paths with __DIR__. Clear the stat cache only when you’ve changed the filesystem. And for critical operations like locking or exclusive creation, let fopen() do the work instead of pre-checking.

These details separate a script that works in testing from one that survives in production.