PHP: What is the best and easiest way to check if

2019-02-12 05:39发布

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?

2条回答
再贱就再见
2楼-- · 2019-02-12 06:27

You can count the items contained in the folder. The first two items are . and .., so just check the items count.

$files_in_directory = scandir('path/to');
$items_count = count($files_in_directory);
if ($items_count <= 2)
{
    $empty = true;
}
else {
    $empty = false;
}
查看更多
叼着烟拽天下
3楼-- · 2019-02-12 06:30

Use glob :

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

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

查看更多
登录 后发表回答