How to tell if the directory contains files, not empty directories
This post was last edited by maniachhz on 2012-11-17 11:40:04
I want to determine if the directory/upload contains files (not directories), but I create a new test directory under/upload (no files), the program executes the result is "not empty", not "empty". Where is the problem, please? Ask for advice
/*
* Traverse directory, if NULL returns false
*/
function Isemptydir ($path)
{
$DH = Opendir ($path);
$r = false;
while (false!== ($f =readdir ($DH)))
{
if ($f!== "." && $f!== ":" && Is_dir ($f)) {
Isemptydir ($f);
}
else if ($f!== "." && $f!== "..")
return $r = true;
}
Closedir ($DH);
return $r;
}
if (Isemptydir ($upload _dir. /")){
echo "Not Empty";
}
else echo "is empty";
------Solution--------------------
Logically, you have no idea:
if ($f!== "." && $f!== ":" && Is_dir ($f)) {
Suppose you go into this branch and think about what effect it will have? This code does not affect the final result.
Isemptydir ($f);
Return Isemptydir ($f);
}
------Solution--------------------
function Isemptydir ($path)
{
$DH = Opendir ($path. ' /');
$r = false;
while (false!== ($f =readdir ($DH)))
{
------Solution--------------------
$f = = "..") Continue
$f = "$path/$f";
if (Is_dir ($f)) {
$r = Isemptydir ($f);
}
else $r = true;
}
Closedir ($DH);
return $r;
}
------Solution--------------------
References: Owner
's code perfect
I modified the function I wrote above. It's still not working. I don't know what's wrong.
PHP code?123456789101112131415161718192021/* * Traverses the directory if NULL returns false */function Isemptydir ($path) {$DH = Opendir ($path. ' /'); $r = false; Wh ...
if ($f!== "." && $f!== ":" && Is_dir ($subdir)) {
Isemptydir ($subdir);//change to "= Isemptydir ($subdir);
}
------Solution--------------------
Citation:
the code perfect
I modified the function I wrote above. It's still not working. I don't know what's wrong.
PHP code?123456789101112131415161718192021/* * Traverses the directory if NULL returns false */function Isemptydir ($path) {$DH = Opendir ($path. ' /'); $r = false; Wh ...
Someone has to take the return value of the recursive call!