PHP Delete the directory and the directory of all the files of the method detailed _php tips

Source: Internet
Author: User
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 ');
?>

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.