Javascript required
Skip to content Skip to sidebar Skip to footer

Upload Documents Php and Check if Exists

Read Time: 5 mins Languages:

Many times y'all will either need to motion files around or store some information inside them in PHP. In either instance, knowing beforehand whether a file exists or not can help us avoid some unexpected behavior.

PHP comes with a diverseness of functions to handle different types of queries related to files. In this tutorial, I'll give you a brief overview of all these functions and so that y'all can pick ane that works all-time in your state of affairs.

The Importance of Checking if a File Exists

At that place are a lot of scenarios in which checking if a file exists earlier doing anything else could be important. Let'south say your website allows users to upload image files on your server that they can access subsequently. It'due south fair to assume that there is always a take a chance of a clashing file proper noun if many users are using your service to upload multiple files often.

In such cases, it becomes important to check if at that place is already another file at the location where you want to save the recently uploaded file of a user. You will and so 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.

Let'southward consider another situation where you are supposed to exist appending data to a file in PHP. If the file y'all 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 proper name and store your information within the newly created file. This might be desirable in some situations, just that won't always be the case. So it makes sense to check if the file exists beforehand if y'all already look it to be at that place earlier you lot begin writing your data.

Checking if a File Exists in PHP

There are 3 dissimilar functions that y'all tin can use to check if a file exists in PHP.

The get-go function is file_exists(). This function accepts a single parameter that is the path where your file is located. Proceed in mind that information technology will return truthful for both existing files and directories. This may or may non be sufficient for your needs.

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

output

In the above example, I intentionally named one of the directories squares.zip to bear witness that information technology is of import to exercise your own checks instead of assuming that 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 really 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() tin be used to get some actress information about a file, besides checking if it exists.

As the name suggests, the is_readable() function will check two things: kickoff, that the file or directory actually exists, and 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 suggest you to exist careful when interpreting the return value of these two functions. For example, our first instinct when is_readable() returns simulated is to recall that the file we queried is not readable. Yet, the function also returns false if the file does non exist. It is important to always keep this aspect of these functions in heed.

Beware of Cached Results

The return value that you get back from a phone 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 office, permit'due south say is_file(), could prove you lot dried results.

PHP caches the results of these functions in order to improve operation. This makes certain that multiple calls for querying the same file work faster. Nevertheless, their return values will stay the same even if the file changes during the class of script execution.

The results are but cached for files that already exist. This means that calling a role is_file() will go on returning fake for non-existing files but will start returning true as soon equally the file is created. On the other hand, the office volition proceed returning true for a file that existed during the outset call, even after the file has been deleted.

If you run the above code snippet for a file that actually exists and and then delete information technology manually while the script waits, calling is_file() will still return true. Still, yous can get the correct results by but calling clearstatcache() earlier querying for the beingness of the file again.

output

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

Final Thoughts

We began this tutorial by learning the importance of checking the existence of files in PHP. After that, we learned nearly unlike functions that yous can employ to check if a file exists in PHP. We besides learned about the advantages and disadvantages that some of these functions might accept.

As I mentioned towards the end, PHP volition cache the results of some of these function calls to amend operation. Therefore, brand certain that y'all clear the cache earlier doing something important with those files.

Did you notice this post useful?

dewlestoregre.blogspot.com

Source: https://code.tutsplus.com/tutorials/how-to-check-if-a-file-exists-in-php--cms-37074