note 95679 deleted from function.rmdir by danbrown
| From: | danbrown@php.net | Date: | Fri, 15 Jan 2010 16:17:03 +0000 |
| Subject: | note 95679 deleted from function.rmdir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165007@lists.php.net to get a copy of this message | ||
Note Submitter: forums at mail dot bg
----
delTree in comments below doesn't work in windows. Here is the fix - using DIRECTORY_SEPARATOR
instead of '/'
function delTree($dir) {
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == DIRECTORY_SEPARATOR )
delTree( $file );
else
unlink( $file );
}
if (is_dir($dir)) rmdir( $dir );
}
This function however doesn't delete ".svn" directories. I don't know is it
possible with glob, but i rewrote delTree using RecursiveDirectoryIterator (PHP 5 >= 5.1.2),
which does the job.
delTree($dir) {
$dirIterator = new RecursiveDirectoryIterator($dir);
$iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
if ($file->isDir()) {
delTree((string) $file);
} elseif ($file->isFile()) {
unlink($file);
}
}
if (is_dir($dir)) rmdir($dir);
}