How to Check If a File Exists in PHP

The best way to check if a file or directory exists in your PHP application.

Published Categorized as PHP, Web & Code

Let’s suppose that, in your PHP application, you want to check whether a specific file or a given directory exists.

What’s the best way to do that?

file_exists

To check if a file (or directory) exists in PHP, use the file_exists filesystem function:

file_exists(<em>path</em>) : bool

The function, as you can see from the definition above, returns a boolean:

  • If the file (or directory) exists, it will return true
  • Otherwise, it will return false

The file_exists function is supported by PHP 4, PHP 5, PHP 7, and PHP 8, which means you can use it in any modern PHP application without worrying about compatibility.

Keep in mind that this function’s results are cached. This means that, unless you use the clearstatcache function, file_exists may behave unexpectedly and return incorrect information.

(The clearstatcache function, for those of you who may not be familiar with it, clears the file status cache.)

Using the Function

To use the file_exists function, you need to specify the path to the file (or the directory) that you want to look for.

To Output True or False

You can do this either by hardcoding the path as a string:

<?php
// Check if your file exists
echo file_exists("my_file.txt");
?>

Or by passing it onto the function as a parameter:

<?php
// Define the path to your file
$path_to_file = "/path/to/my_file.txt";

// Check if your file exists
echo file_exists($path_to_file);
?>

Passing on the path as a parameter is almost always better than hardcoding it in your function.

By choosing to go this route, you can reuse the function for more than one file or directory, and you can define the paths to those files or directories dynamically. This makes your PHP application easier to maintain and fuss free to extend.

In both of our examples above, the function will output true or false to the screen based on the results.

In a Conditional Check

Instead of outputting the file_exist function’s results to the screen, you can also perform a conditional check with it, allowing you to use the results to run other code:

<?php
// Define the path to your file
$path_to_file = "/path/to/my_file.txt";

// Check if your file exists
// If it does, do this
if (file_exists($path_to_file)) {
    echo "Hooray, the file exists!";
}
// If it doesn't, do this
else {
    echo "Whoops, no such file exists!";
}
?>

Most of the time, you will probably be doing conditional checks with the file_exists function rather than outputting its results to the screen.

After all, the value of this function isn’t in the boolean that it returns, but in the logic it allows you to develop based on it.

Clearing the Cache

As I mentioned in the beginning of this article, the results of the file_exists function are cached.

If you’ve ran file_exists once for a file or directory and something changes (for example, the file or directory is created or deleted after that), it will return incorrect information unless you clear the cache using the clearstatcache before running it again.

Here’s how this works with our simpler, hardcoded example:

<?php
// Clear the cache
clearstatcache();

// Check if your file exists
echo file_exists("my_file.txt");
?>

As you can expect, it works the same way with our more complicated, dynamic example:

<?php
// Clear the cache
clearstatcache();

// Define the path to your file
$path_to_file = "/path/to/my_file.txt";

// Check if your file exists
echo file_exists($path_to_file);
?>

As well as with our conditional check:

<?php
// Clear the cache
clearstatcache();

// Define the path to your file
$path_to_file = "/path/to/my_file.txt";

// Check if your file exists
// If it does, do this
if (file_exists($path_to_file)) {
    echo "Hooray, the file exists!";
}
// If it doesn't, do this
else {
    echo "Whoops, no such file exists!";
}
?>

Hope this helped!

If you have any questions, leave a comment below.

Image courtesy of eric1513 /Depositphotos

Leave a comment

Your email address will not be published. Required fields are marked *