PHP Recursive Delete directory _php tutorial

Source: Internet
Author: User
Excerpt from "Linuxer" blog http://deidara.blog.51cto.com/400447/118805

To delete an empty directory is simple ~ a

RmDir () function can be done, but to delete a non-empty directory, you will not be able to quickly delete, you must first delete the directory files, but there may be subdirectories in the directory so to be recursive delete ~ here is my example ~


function Deletedir ($dir) {
if (!handle= @opendir ($dir)) {//detects if the directory to open exists
Die ("No such directory");
}
while (False!== ($file =readdir ($handle))) {
if ($file!== "." && $file!== "..") {//Exclude current directory from parent directory
$file = $dir. Directory_separator. $file;
if (Is_dir ($file)) {
Deletedir ($file);
}else{
if (@unlink ($file)) {
echo "File $file deleted successfully.
";
}else{
echo "File $file Delete failed!
";
}
}
}
if (@rmdir ($dir)) {
echo "Directory $dir deleted successfully.
";
}else{
echo "Directory $dir Delete failed!
";
}
}

Test program
$dir = "/var/www/test";
Deletedir ($dir);
?>


Create a Write folder and file test under the/var/www/test folder
Shell> Touch AAA
Shell> Touch BBB
Shell> Touch CCC
Shell> Touch eee
Shell> Touch FFFF
Shell> mkdir 111
shell> mkdir 222
shell> mkdir 333
Write files in the 111,222,333 folder, respectively. There's not much to say, and then give them permission.
Shell>chown [Url]www.www[/url] Test-r
Then open the program in IE test it ~ hehe.

http://www.bkjia.com/PHPjc/508519.html www.bkjia.com true http://www.bkjia.com/PHPjc/508519.html techarticle excerpt from "linuxer" blog http://deidara.blog.51cto.com/400447/118805 to delete an empty directory is very simple ~ a rmdir () function can be done, but to delete a non-empty directory, ...

  • Related Article

    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.