Skip to main content
Simplify expression
Source Link
Markus Zeller
  • 9.3k
  • 2
  • 34
  • 39

Do a scandir and count the contents. An empty directory always contains . and ... So if the count is greater than 2 it is not empty.

function isDirectoryEmpty(string $dir): bool
{
    ifreturn (is_dir($dir)) {
        return&& count(scandir($dir)) <= 2;
    }

    return false;
}

Do a scandir and count the contents. An empty directory always contains . and ... So if the count is greater than 2 it is not empty.

function isDirectoryEmpty(string $dir): bool
{
    if (is_dir($dir)) {
        return count(scandir($dir)) <= 2;
    }

    return false;
}

Do a scandir and count the contents. An empty directory always contains . and ... So if the count is greater than 2 it is not empty.

function isDirectoryEmpty(string $dir): bool
{
    return is_dir($dir) && count(scandir($dir)) <= 2;
}
Source Link
Markus Zeller
  • 9.3k
  • 2
  • 34
  • 39

Do a scandir and count the contents. An empty directory always contains . and ... So if the count is greater than 2 it is not empty.

function isDirectoryEmpty(string $dir): bool
{
    if (is_dir($dir)) {
        return count(scandir($dir)) <= 2;
    }

    return false;
}