Share two usage functions: PHP loop Delete directories and directories under the file and only delete files under the specified directory, do not delete the directory folder!
Code One: PHP loop delete directories and files under the directory
<?php
Loop Delete directory and file functions
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 files: $dirName/$item <br/>n";
}
}
}
Closedir ($handle);
if (RmDir ($dirName)) echo "Successfully deleted directory: $dirName <br/>n";
}
}
?>
Code two: Delete only the files under the specified directory, do not delete the directory folder.
All files in the loop directory
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 files: $dirName/$item <br/>n";
}
}
}
Closedir ($handle);
}
}
How to call it needless to say, Haha, a friend who encounters a similar problem can try the amount!