Bueno ya que estamos te dejo una version OOP con iterators,
para eliminar
Código PHP:
Ver originalclass DeleteRecursiveDirectory
{
/**
* @var string
*/
protected $_path;
/**
* @var RecursiveIteratorIterator
*/
protected $_iterator;
/**
* DeleteRecursiveDirectory constructor
* @param string $path
* @throws RuntimeException
*/
public function __construct($path)
{
$this->_iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->_path, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST);
}
public function delete()
{
foreach($this->_iterator as $item){
$path = $item->getPathname();
}
}
}
modo de empleo:
Código PHP:
Ver originaltry {
$toDel = new DeleteRecursiveDirectory('/path/to/directory');
$toDel->delete();
} catch(RuntimeException $e) {
echo $e->getMessage() . PHP_EOL;
}
para listar:
Código PHP:
Ver originalclass DirectoryTreeIterator extends RecursiveIteratorIterator
{
/**
* DirectoryTreeIterator constructor
* @param string $path
* @throws UnexpectedValueException
* @throws RuntimeException
*/
public function __construct($path)
{
parent::__construct(
new RecursiveCachingIterator(
new RecursiveDirectoryIterator
(realpath($path), RecursiveDirectoryIterator
::KEY_AS_FILENAME), CachingIterator::CALL_TOSTRING|CachingIterator::CATCH_GET_CHILD
),
parent::SELF_FIRST
);
}
/**
* @return string
*/
{
$tree = '';
for ($l=0; $l < $this->getDepth(); $l++) {
$tree .= $this->getSubIterator($l)->hasNext() ? '| ' : ' ';
}
return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-')
. $this->getSubIterator($l)->__toString();
}
public function __call($func, $params)
{
}
}
modo de empleo:
Código PHP:
Ver originaltry{
$it = new DirectoryTreeIterator('/path/to/directory/');
foreach($it as $path) {
echo $path . PHP_EOL;
}
}catch(UnexpectedValueException $e){
echo $e->getMessage() . PHP_EOL;
}catch(RuntimeException $e){
echo $e->getMessage() . PHP_EOL;
}
Saludos.