How to check If directory exists in PHP?

How to check If directory exists in PHP?

It’s always a good practice to validate a directory or directory path before using that in the code. There could be multiple options in PHP to validate a directory like one using the shell_exec and other one is using the inbuilt is_dir function.

We will look at both of the solutions in this post.

Validate Directory using shell_exec or exec

Now, let’s have a look at another way of doing it.

Validate Directory using is_dir

Note: clearstatcache is to clear the file status cache. PHP caches the return when we use file_exists and is_dir, to provide the faster performance. So we should clear that before validating a directory. Although, we need to call clearstatcache only If we are performing multiple operations on the same filename and we don’t want to cache that information.

You might also like How to validate email in PHP? or How to validate link in PHP?

Leave a Reply

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