PHP: What is the best and easiest way to check if directory is empty or not

ErickBest picture ErickBest · Sep 8, 2013 · Viewed 17.6k times · Source

I got a root directory with 100s of dynamically generated folders. As time goes some of these folders will need to be extirpated from system on the condition that this(ese) directories(s) must be empty. What would be the best shortest, easiest and/or most effective way to achieve that?

Answer

user2625787 picture user2625787 · Sep 8, 2013

Use glob :

if (count(glob("path/*")) === 0 ) { // empty

A nice thing about glob is that it doesn't return . and .. directories.