Three kinds of PHP delete all files and directories under the specified directory

Source: Internet
Author: User
The code is as follows Copy Code
function Deltreedir ($dir) {
$dir = Realpath ($dir);
if (! $dir | |! @is_dir ($DIR))
return 0;
$handle = @opendir ($dir);
if ($dir [strlen ($dir)-1]!= directory_separator)
$dir. = Directory_separator;
while ($file = @readdir ($handle)) {
if ($file!= '. ' && $file!= ' ... ') {
if (@is_dir ($dir. $file) &&!is_link ($dir. $file))
Deltreedir ($dir. $file);
Else
@unlink ($dir. $file);
}
}
Closedir ($handle);
@rmdir ($dir);
}

Example Two

To recycle directory and file functions

The code is as follows Copy Code
function Deldirandfile ($dirName)
{
if ($handle = Opendir ("$dirName")) {
while (false!== ($item = Readdir ($handle))) {
if ($item!= "." && $item!= "...") {
if (Is_dir ("$dirName/$item")) {
Deldirandfile ("$dirName/$item");
} else {
if (unlink ("$dirName/$item")) echo "successfully deleted the file: $dirName/$item <br/>";
}
}
}
Closedir ($handle);
if (RmDir ($dirName)) echo "successfully deletes the directory: $dirName <br/>";
}
}

Example Three

The code is as follows Copy Code
function Delfileunderdir ($dirName = ". /smarty/templates/templates_c ")
{
if ($handle = Opendir ("$dirName")) {
while (false!== ($item = Readdir ($handle))) {
if ($item!= "." && $item!= "...") {
if (Is_dir ("$dirName/$item")) {
Delfileunderdir ("$dirName/$item");
} else {
if (unlink ("$dirName/$item")) echo "successfully deleted the file: $dirName/$item <br/>";
}
}
}
Closedir ($handle);
}
}

Deldirandfile (' www.111cn.net ');

Above three kinds of delete directories and directories under all the files and empty directory instances, are to traverse to do, that is, the return of a speech again to judge on OK.

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.