note 86112 deleted from function.rmdir by aidan
| From: | aidan@php.net | Date: | Wed, 15 Apr 2009 04:20:50 +0000 |
| Subject: | note 86112 deleted from function.rmdir by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153229@lists.php.net to get a copy of this message | ||
Note Submitter: scrappingcc at googlemail dot com
----
here is a little improved 'removing' function with DirectoryIterator:
note for PHP on windows: if using the iterator and recursively removing dirs and files, u need to
unset() before leaving/continue the foreach! ( permission denied problem...)
<?php
function removeRessource( $_target ) {
//file?
if( is_file($_target) ) {
if( is_writable($_target) ) {
if( @unlink($_target) ) {
return true;
}
}
return false;
}
//dir?
if( is_dir($_target) ) {
if( is_writeable($_target) ) {
foreach( new DirectoryIterator($_target) as $_res ) {
if( $_res->isDot() ) {
unset($_res);
continue;
}
if( $_res->isFile() ) {
removeRessource( $_res->getPathName() );
} elseif( $_res->isDir() ) {
removeRessource( $_res->getRealPath() );
}
unset($_res);
}
if( @rmdir($_target) ) {
return true;
}
}
return false;
}
}
?