How to remove all files in directory and directory by PHP _php tutorial

Source: Internet
Author: User
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 ...

  • Related Article

    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.