How to Check if a File Exists in PHP

Many times you will either need to move files around or store some data inside them in PHP. In either case, knowing beforehand whether a file exists or not can help us avoid some unexpected behavior.

PHP comes with a variety of functions to handle different types of queries related to files. In this tutorial, we will give you a brief overview of all these functions so that you can pick one that works best in your situation.

Importance of Checking if a File Exists

There are a lot of scenarios in which checking if a file exists before doing anything else could be important. Lets say your website allows users to upload image files on your server that they can access later. It is fair to assume that their is always a chance of a clashing file name if many users are using your service to upload multiple files frequently.

In such cases, it becomes important to check if there is already another file at the location where you want to save the recently uploaded file of a user. You will then have the option to take some steps like renaming the file to something else or letting the user know that their upload will overwrite an existing file.

Lets consider another situation where you are supposed to be appending data to a file in PHP. If the file you created to write all your data is deleted for some reason, functions like file_put_contents() will just create a new file with the specified name and store your data inside the newly created file. This might be desirable in some situations but that won’t always be the case. So, it makes sense to check if the file exists beforehand if you already expect it to be there before you begin writing your data.

Checking if a File Exists in PHP

There are three different functions that you can use to check if a file exists in PHP.

The first function is file_exists(). This function accepts a single parameter that is the path where you file is located. Keep in mind that it will return true for both existing files as well as directories. This may or may not be sufficient for your needs.

You can consider using the is_file() function if you want to be sure that the path you specified points to a file and not a directory. Similarly, you can use the is_dir() function to check if the path you specified exists and if it points to a directory.

output

In the above example, I intentionally named one of the directories squares.zip to show that it is important to do your own checks instead of assuming if the provided filename is actually a filename or a directory.

It is important to remember that both is_file() and is_dir() will return false even for paths that actually exist when the parent directory does not have the right permissions.

Check if File Exists and is Readable or Writable

Two more functions named is_readable() and is_writable() can be used to get some extra information about a file besides checking it is exists.

As the name suggests, the is_readable() function will check two things. First, that the file or directory actually exists. Second, that the file is readable. Similarly, the is_writable() function also checks two things, that the file or directory exists and is writable.

output

I would advise you to be careful when interpreting the return value of these two functions. For example, our first instinct when is_readable() returns false it to think that the file we queried is not readable. However, the function also returns false if the file does not exist. It is important to always keep this aspect of these functions in mind.

Beware of Cached Results

The return value that you get back from a call to all these five functions, namely, file_exists(), is_file(), is_dir(), is_readable() and is_writeable() is cached. This means that repeated calls to a function, let’s say is_file() could show you stale results.

PHP caches the result of these function in order to improve performance. This makes sure that multiple calls for querying the same file work faster. However, their return values will stay the same even if the file changes during the course of script execution.

The results are only cached for files that already exist. This means that calling a function is_file() will keep returning false for non-existing files but start returning true as soon as the file is created. On the other hand, the function will keep returning true for a file that existed during the first call even after the file has been deleted.

If you run the above code snippet for a file that actually exists and then delete it manually while the script waits, calling is_file() will still return true. However, you can get the correct results by simply calling clearstatcache() before querying for the existence of the file again.

output

Another thing the remember is that a call to unlink() automatically clears the cache so you get fresh results for calls to functions like is_file() later.

Final Thoughts

We began this tutorial by learning the importance of checking existence of files in PHP. After that, we learned about different functions that you can use to check if a file exists in PHP. We also learned about advantages and disadvantages that some of these functions might have.

As I mentioned towards the end, PHP will cache the results of some of these function calls to improve performance. Therefore, make sure that you clear the cache before doing something important with those files.

Leave a comment

Your email address will not be published.