Function code One: delete all files in directory and directory
Copy Code code as follows:
To recycle 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 deletes the file: $dirName/$item
\ n ";
}
}
}
Closedir ($handle);
if (RmDir ($dirName)) echo successfully deletes the directory: $dirName
\ n ";
}
}
?>
function Code Two: Deletes only the files in the specified directory, and does not delete the directory folder.
Copy Code code as follows:
All files under the looping directory
function Delfileunderdir ($dirName)
{
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 deletes the file: $dirName/$item
\ n ";
}
}
}
Closedir ($handle);
}
}
?>
examples of using methods:
Suppose you need to delete a sibling named "upload" that is all of the files in this directory, you can do this by using the following code:
Copy Code code as follows:
Deldirandfile (' upload ');
?>
Suppose you need to delete all the files in the directory named "Upload" (but you do not need to delete the directory folder), you can do this by using the following code:
Delfileunderdir (' upload ');
?>