How to delete a multilevel directory _php base

Source: Internet
Author: User
Yesterday I saw a post (chinaasp) asked how to delete the directory, has been able to, but yesterday, the problem, originally he just put his subordinate documents deleted after the deletion

Table of Contents So if there are more than one.

My this can only be used for a while, if your directory does not have more than 10 layers of words should be no problem ~, but I am not familiar with recursion can only do

deltree ($path); RmDir ($path) can delete this directory can not be directly deltree ($path), delete this directory??

function deltree ($pathdir)
{
echo $pathdir;//I use it when I'm debugging.

if (Is_empty_dir ($pathdir))//If Empty
{
RmDir ($pathdir);//delete directly
}
Else
{//otherwise read this directory, except ... and. Outside
$d =dir ($pathdir);
while ($a = $d->read ())
{
if (Is_file ($pathdir. ') /'. $a) && ($a!= '. ') && ($a!= ' ... ')) {unlink ($pathdir. ') /'. $a);}
If it's a file, delete it directly.
if (Is_dir ($pathdir. ') /'. $a) && ($a!= '. ') && ($a!= ' ... '))
{//If it is a directory
if (!is_empty_dir ($pathdir. ') /'. $a))/is empty
{//If not, call itself, but the original path + his subordinate directory name
deltree ($pathdir. ' /'. $a);
}
if (Is_empty_dir ($pathdir. ') /'. $a))
{//If it is empty, delete it directly
RmDir ($pathdir. ' /'. $a);
}
}
}
$d->close ();

echo "must first delete all files under the directory";//I use it when I debug

}

}

function Is_empty_dir ($pathdir)
{//To determine if the directory is empty, my method is not very good? Just to see, except. There's something else that isn't empty, PHP has nothing to give

Function?
$d =opendir ($pathdir);
$i = 0;
while ($a =readdir ($d))
{
$i + +;
}
Closedir ($d);
if ($i >2) {return false;}
else return true;
}

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.