Function code One: delete all files in directory and directory
Copy CodeThe code is as follows:
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 file: $dirName/$item
\ n ";
}
}
}
Closedir ($handle);
if (RmDir ($dirName)) echo "Successfully deleted directory: $dirName
\ n ";
}
}
?>
function code Two: Delete only the files in the specified directory, do not delete the directory folder.
Copy CodeThe code is as follows:
All files in the loop 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 deleted file: $dirName/$item
\ n ";
}
}
}
Closedir ($handle);
}
}
?>
Examples of Use methods:
Suppose you need to delete a sibling directory named "upload" that is all the files in this directory, which you can do with the following code:
Copy CodeThe code is as follows:
Deldirandfile (' upload ');
?>
Suppose you need to delete all the files in the directory named "Upload" (but without deleting the directory folder), you can do this with the following code:
Delfileunderdir (' upload ');
?>
http://www.bkjia.com/PHPjc/327460.html www.bkjia.com true http://www.bkjia.com/PHPjc/327460.html techarticle function Code One: delete the directory and all files under the directory Copy Code code as follows://Loop Delete directory and file function Deldirandfile ($dirName) {if ($handle = Opendir ...