How to tell if the directory contains files, not empty directories

Source: Internet
Author: User
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!
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.