PHP Delete all files in directory

Source: Internet
Author: User

*/

Delete File method in directory one

The code is as follows Copy Code

function Clean_dir ($path) {
if (!is_dir ($path)) {
if (Is_file ($path)) {
Unlink ($path);
}
Return
}
$p =opendir ($path);
while ($f =readdir ($p)) {
if ($f = = "." | | $f = = "...") Continue
Clean_dir ($path. $f);
}
RmDir ($path);
Return
}
//

function Delfile ($path, $level = 0) {

$path $file

Delete all file instances under directory two

$file = "";

$lev = 0;

$dir = @opendir ($path);

while ($con = @readdir ($dir)) {

if ($con!= "." && $con!= "..." && $con!= "") {

$file = $path. " /". $con;

if (Is_dir ($file)) {

$lev = Delfile ($file, $level + +);

@rmdir ($file);

} else {

@unlink ($file);

}

}

}
for ($i =0; $i < $lev; $i + +) {

Delfile ($path, $level);

}

@rmdir ($path);

return $level;

}

Delfile ("./www.111cn.net");


Directory traversal function

The code is as follows Copy Code
function DirTree ($path = "./test") {
echo "<dl>";
$d = Dir ($path);
while (false!== ($v = $d->read ())) {
if ($v = = "."
$v = = "...")
Continue
$file = $d->path. " /". $v;
echo "<dt> $v";
if (Is_dir ($file))
DirTree ($file);
}
$d->close ();
echo "</dl>";
}

DirTree ();

//php file system functions are much better than ASP tutorials, delete directory All files as long as the unlink on the line, we just to the directory to traverse the is_file on the OK.
//This site to collate the reprint annotated www.111cn.net

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.