A class to do recursive directory actions.
Only works on PHP5
<?php /*
//remove directory recursively
directoryUtility::remove($dirname);
//scan directory recursively
$dir = directoryUtility::scan($dirname));
print_r($dir);
*/ class directoryUtility {
function scan($dirname)
{
$array = array();
$dir = new RecursiveDirectoryIterator($dirname);
foreach($dir as $k => $v)
{
if(!$dir->isDot())
{
$array[]=$v->getPathname();
}
if($v->isDir())
{
$subArray = directoryUtility::scan($v->getPathname());
$array = array_merge($array,$subArray);
}
}
return $array;
}
function remove($dirname)
{
if(is_dir($dirname))
{
$dir = new RecursiveDirectoryIterator($dirname);
foreach($dir as $k => $v)
{
if(!$dir->isDot())
{
if($v->isDir()&&$v->hasChildren())
{
directoryUtility::remove($v->getPathname());
}
else
{
unlink($v->getPathname());
}
}
}
unset($dir);
rmdir($dirname);
return true;
}
return false;
}
} ?>
Sign up to add your own comment here!
Comments
|
The method 'remove' doesn't work. One issue I found is the line:
if($v->isDir()&&$v->hasChildren())
The variable $v is of class SplFileInfo which doesn't have a 'hasChildren' method.
I removed the hasChildren() test and the code now appears to work properly. The replacement line now reads:
if($v->isDir()) |
More comments: 1
|
|