Skip to main content
added an explanation for why this works
Source Link
mindplay.dk
  • 7.5k
  • 3
  • 51
  • 60

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

This works because a new FilesystemIterator will initially point to the first file in the folder - if there are no files in the folder, valid() will return false. (see documentation here.)

As pointed out by abdulmanov.ilmir, optionally check if the directory exists before using the FileSystemIterator because otherwise it'll throw an UnexpectedValueException.

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

As pointed out by abdulmanov.ilmir, optionally check if the directory exists before using the FileSystemIterator because otherwise it'll throw an UnexpectedValueException.

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

This works because a new FilesystemIterator will initially point to the first file in the folder - if there are no files in the folder, valid() will return false. (see documentation here.)

As pointed out by abdulmanov.ilmir, optionally check if the directory exists before using the FileSystemIterator because otherwise it'll throw an UnexpectedValueException.

Added note about UnexpectedValueException
Source Link
flu
  • 14.9k
  • 8
  • 79
  • 73

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

As pointed out by abdulmanov.ilmir, optionally check if the directory exists before using the FileSystemIterator because otherwise it'll throw an UnexpectedValueException.

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

As pointed out by abdulmanov.ilmir, optionally check if the directory exists before using the FileSystemIterator because otherwise it'll throw an UnexpectedValueException.

added 17 characters in body
Source Link
flu
  • 14.9k
  • 8
  • 79
  • 73

I usethink using the FilesystemIterator should be the fastest and easiest way:

$isDirEmpty// PHP 5 >= 5.3.0
$iterator = !array_diff(scandirnew \FilesystemIterator($dir),;
$isDirEmpty array= !$iterator->valid('.',);

Or using class member access on instantiation:

// 'PHP 5 >= 5.4.'0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();

Since glob only finds hidden files if you explicitly ask it for this seems to be the easiest way.

I use:

$isDirEmpty = !array_diff(scandir($dir), array('.', '..'));

Since glob only finds hidden files if you explicitly ask it for this seems to be the easiest way.

I think using the FilesystemIterator should be the fastest and easiest way:

// PHP 5 >= 5.3.0
$iterator = new \FilesystemIterator($dir);
$isDirEmpty = !$iterator->valid();

Or using class member access on instantiation:

// PHP 5 >= 5.4.0
$isDirEmpty = !(new \FilesystemIterator($dir))->valid();
Source Link
flu
  • 14.9k
  • 8
  • 79
  • 73
Loading