note 44913 deleted from function.unlink by tomsommer
| From: | tomsommer@php.net | Date: | Thu, 19 Aug 2004 23:16:22 +0000 |
| Subject: | note 44913 deleted from function.unlink by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74962@lists.php.net to get a copy of this message | ||
Note Submitter: affenschlaffe web-de
----
This is gman's function but with some changes.
1. WARNING: This deletes the directory immediately, you don't have to call rmdir after deltree.
Should not be too bad, because it should be empty anyways :-)
2. Don't fail on empty directories.
3. Don't require "/" at the end of the the argument to deltree.
Improvements welcome.
Example / Code:
<?php
$dir=$_SERVER['DOCUMENT_ROOT']."/foo/$bar";
deltree($dir);
function deltree($dir) {
function recursive_del($dir) {
$d = dir($dir);
while($f = $d->read() ) {
if($f != "." && $f != "..") {
if(is_dir($dir.$f)) {
deltree($dir.$f."/");
rmdir($dir.$f);
}
debug_out($dir.$f);
if(is_file($dir.$f))
unlink($dir.$f);
}
}
$d->close();
}
if (is_dir($dir)) {
if (($dir{strlen($dir)-1})!='/')
$dir.='/';
recursive_del($dir);
rmdir($dir);
}
else {
return;
}
}
?>