It seems you need scandir instead of glob, since glob cannot see hidden unix files.
<?php $pid = basename($_GET["prodref"]); //let sanitize it a bit $dir = "/assets/$pid/v"; if (is_dir_empty($dir)) { echo "the folder is empty"; }else{ echo "the folder is NOT empty"; } function is_dir_empty($dir) { if (!is_readable($dir)) return NULL; return (count(scandir($dir)) == 2); } ?>
Note that this code is not the pinnacle of efficiency, since there is no need to read all the files just to indicate if the directory is empty. So the best version will be
function dir_is_empty($dir) { $handle = opendir($dir); while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { return FALSE; } } return TRUE; }
By the way, do not use words to replace logical values. The very purpose of the latter is to tell you that something is empty or not.
a === b
the expression already returns Empty or Non Empty in terms of a programming language, FALSE or TRUE respectively - so that you can use the result itself in control structures such as IF() without any intermediate values
Your Common Sense Sep 21 '11 at 9:52 2011-09-21 09:52
source share