Many times you have to either transfer the files or store some data inside them in PHP. In either case, knowing in advance whether a file exists or not can help us avoid unexpected behavior.

PHP includes several functions to handle different types of file-related queries. In this tutorial, we’ll give you a brief overview of all of these features so you can choose the one that works best for your situation.

The importance of checking the existence of a file

There are many scenarios where checking the existence of a file before doing anything else can be important. It is said that your website allows users to upload image files to your server for later use. It is reasonable to assume that their name will always conflict with the file name if many users use your service to download multiple files frequently.

In such cases, it is important to check if there is already another file in the location where you want to save the user’s recently downloaded file. Then you have the option to do some steps, such as rename the file or let the user know that their download will replace the existing file.

Consider another situation where it is assumed to attach data to a PHP file. If the file you created to write all the data is deleted for some reason, it works like file_put_contents() only creates a new file with the specified name and saves your information to a newly created file. This may be desirable in some situations, but it is not always the case. So it makes sense to check if the file exists in advance if you have already waited for it to be there before you start writing your data.

Check if there is a file in PHP

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

The first function is file_exists(). This function accepts one parameter, which is the path where the file is located. Remember that it is true for both existing files and directories. This may or may not be enough for your needs.

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

output

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

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

Check to see if the file exists and is readable or writable

Two other functions named is_readable() and is_writable() can be used to obtain additional information about the file in addition to checking its existence.

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

output

I urge you to be careful when interpreting the return value of these two functions. For example, the first instinct when is_readable() returns false by assuming that the file we are querying is not readable. However, the function returns false if the file does not exist. It is important to always keep these functions in mind.

Watch out for cached results

The return value you get back from the call to all five of these functions, namely file_exists(), is_file(), is_dir(), is_readable() and is_writeable() is cached. This means that repeated calls to a function, it is said is_file() may show you outdated results.

PHP caches the results of these operations to improve performance. This ensures that multiple calls to query the same file run faster. However, the recovery values ​​remain the same even if the file changes during script execution.

The results are cached only for existing files. This means calling a function is_file() is constantly lit. false for non-existent files, but starts to revert true as soon as the file is created. On the other hand, the function is lit continuously true to a file that existed during the first call, even if the file was deleted.

If you run the above code snippet on an existing file and delete it manually while the script is waiting, is_file() still lit. true. However, you can get the right results by simply calling clearstatcache() before re-inquiring about the existence of the file.

output

Another thing to remember is the invitation unlink() automatically clears the cache to get new results for calls, for example is_file() later.

Final thoughts

We started this tutorial by learning the importance of checking the existence of files in PHP. After that, we will learn about different functions that you can use to check if the file is in PHP. We also learn about the pros and cons that some of these features may have.

As I mentioned at the end, PHP caches the results of some of these function calls to improve performance. Therefore, be sure to clear your cache before doing anything important with these files.

LEAVE A REPLY

Please enter your comment!
Please enter your name here