How can I use PHP to check if a directory is empty?

How can I use PHP to check if a directory is empty?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Trap in Checking for an Empty Directory

The script you provided demonstrates a classic edge case in file system logic: the difference between "no files" and "no entries." Your current implementation uses glob() to retrieve a list of items within the directory, then checks if that count is zero. While this works in many scenarios, it fails when hidden files are present.

In Unix-like systems (including Linux and macOS), files beginning with a dot (.) are treated as hidden by default. When you execute glob("$dir/*"), the pattern * explicitly matches any file except those starting with a dot. Consequently, if your directory contains only .gitignore, .htaccess, or other configuration files, glob() returns an empty array. Your script interprets this empty result as an "Empty" status, even though files physically exist on the disk.

To resolve this, you must explicitly include hidden files in your search pattern. The simplest adjustment is to change the glob pattern from "$dir/*" to "$dir/.*" or better yet, combine them to ensure both visible and hidden entries are counted. A robust approach uses a single pattern that matches everything:

$entries = glob($dir . '/*', GLOB_BRACE);
// However, standard glob doesn't always catch hidden files with just '*' 
// because '*' excludes leading dots by default in many contexts depending on OS behavior.

// The most reliable method for a generic check is to explicitly include the dot prefix:
$pattern = $dir . '/*' . '||' . $dir . '/.*';
$count = count(glob($pattern));

$q = ($count === 0) ? 'Empty' : 'Not empty';

Alternatively, you can use a single pattern that matches everything by including the dot character in the wildcard logic, or simply iterate through the directory using scandir(). The scandir() function is often preferred for this specific task because it returns an array of all files and directories within the path, including hidden ones, without requiring complex regex patterns.

$dir = '/assets/' . $pid . '/v';
$files = scandir($dir);

// scandir includes '.' and '..' which represent the current and parent directory.
// You must filter these out to get a true count of actual items.
$count = count(array_filter($files, function ($file) {
    return $file !== '.' && $file !== '..';
}));

$q = ($count === 0) ? 'Empty' : 'Not empty';

Using scandir() ensures that whether the directory contains .htaccess, index.html, or a mix of both, your script will accurately reflect the state of the filesystem. This is a critical distinction because relying on glob() without accounting for hidden files introduces silent logic errors that only surface when specific file types are present.

Another consideration is permission handling. If the executing user lacks read permissions on the directory, scandir() or glob() will return an empty array regardless of whether files exist. Always wrap these checks in error suppression (@) or explicit exception handling if you cannot guarantee environment permissions, though modern PHP practices often favor explicit error reporting to catch permission issues early during development.

If you are working within a Laravel application, you might consider using the Storage facade for filesystem abstraction, which handles path normalization and permission logic internally. However, for raw system checks like this, native PHP functions remain the most performant and direct solution. The key takeaway is understanding that * does not mean "everything" in the context of hidden files; you must explicitly define what constitutes a valid entry.

Our network