PHP Delete a non-empty directory Function code summary _php Tutorial

Source: Internet
Author: User
With this applet, phper do not have to manually delete the directory files on the computer, in the practice of PHP directory file operation can use this function, on this basis can also increase the Browse folder directory, and then delete.

Code One:
Copy CodeThe code is as follows:
function D_rmdir ($dirname) {//Delete non-empty directory
if (!is_dir ($dirname)) {
return false;
}
$handle = @opendir ($dirname);
while (($file = @readdir ($handle))!== false) {
if ($file! = '. ' && $file! = ' ... ') {
$dir = $dirname. '/' . $file;
Is_dir ($dir)? D_rmdir ($dir): unlink ($dir);
}
}
Closedir ($handle);
return rmdir ($dirname);
}
if (D_rmdir ("./temp"))
echo "Succes";
Else
echo "false";
?>


The second one was flipped out from the manual:)

Code two:
Copy CodeThe code is as follows:
 
Functionremove_directory ($dir) {
if ($handle =opendir ("$dir")) {
while (false!== ($item =readdir ($handle))) {
if ($item! = "." && $item! = "...") {
if (Is_dir ("$dir/$item")) {
Remove_directory ("$dir/$item");
}else{
Unlink ("$dir/$item");
echo "removing$dir/$item
";
}
}
}
Closedir ($handle);
RmDir ($dir);
echo "Removing$dir
";
}
}

The third one is codebit.cn, or the manual.

Code Three:

Copy CodeThe code is as follows:
Functionremovedir ($dirName)
{
if (!is_dir ($dirName))
{
Returnfalse;
}
$handle = @opendir ($dirName);
while (($file = @readdir ($handle))!==false)
{
if ($file! = '. ') && $file! = ' ... ')
{
$dir = $dirName. ' /'. $file;
Is_dir ($dir)? RemoveDir ($dir): @unlink ($dir);
}
}
Closedir ($handle);
Returnrmdir ($dirName);
}
?>

http://www.bkjia.com/PHPjc/326565.html www.bkjia.com true http://www.bkjia.com/PHPjc/326565.html techarticle with this small program, phper do not have to manually delete the directory files on the computer, in the practice of PHP directory file operation can use this function, on this basis can also increase the ...

  • 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.