PHP loops Delete directories and files in directories

Source: Internet
Author: User

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!

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.