PHP Delete a non-empty directory function code summary _php tips

Source: Internet
Author: User
With this applet, Phper will 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 Browsing folder directory, and then delete.

Code One:
Copy Code code as follows:

<?php
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 is from the manual:

Code two:
Copy Code code as follows:

<?php
 
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 <br>";
}
}
}
Closedir ($handle);
RmDir ($dir);
echo "Removing$dir<br>";
}
}

The third one is collected on the codebit.cn or in the manual.

Code Three:

Copy Code code 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);
}
?>

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.