Created
February 1, 2013 10:23
-
-
Save ArnaudLigny/4690518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Recursively remove a directory | |
* | |
* @param string $dirname | |
* @param boolean $followSymlinks | |
* @return boolean | |
*/ | |
function RecursiveRmdir($dirname, $followSymlinks=false) | |
{ | |
if (is_dir($dirname) && !is_link($dirname)) { | |
if (!is_writable($dirname)) { | |
throw new Exception(sprintf('%s is not writable!', $dirname)); | |
} | |
$iterator = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($dirname), | |
RecursiveIteratorIterator::CHILD_FIRST | |
); | |
while ($iterator->valid()) { | |
if (!$iterator->isDot()) { | |
if (!$iterator->isWritable()) { | |
throw new Exception(sprintf( | |
'%s is not writable!', | |
$iterator->getPathName() | |
)); | |
} | |
if ($iterator->isLink() && $followLinks === false) { | |
$iterator->next(); | |
} | |
if ($iterator->isFile()) { | |
unlink($iterator->getPathName()); | |
} | |
elseif ($iterator->isDir()) { | |
rmdir($iterator->getPathName()); | |
} | |
} | |
$iterator->next(); | |
} | |
unset($iterator); | |
return rmdir($dirname); | |
} | |
else { | |
throw new Exception(sprintf('%s does not exist!', $dirname)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment