I am trying to use php to check if a directory is empty or not. I have seen a lot of similar questions but I have not been able to solve the problem. Namely, the following code always gives me the result that the directory is not empty:
<?php
function is_dir_empty($dir) {
if (!is_readable($dir)) return NULL;
$files = array_diff(scandir($dir), array('.', '..'));
return empty($files);
}
$dir = 'images/tmp';
if (is_dir_empty($dir)) {
echo "The folder is empty";
} else {
echo "The folder is NOT empty";
}
?>
the directory permissions are 755 and the path is correct. I also tried with this function but the result is the same:
function is_dir_empty($dir) {
if (!is_readable($dir)) return NULL;
$files = glob("$dir/*");
return empty($files);
}