public function recursiveRemoveDirectory($directory, $empty = false)
{
// if the path has a slash at the end we remove it here
if (substr($directory, -1) === '/') {
$directory = substr($directory, 0, -1);
}
// if the path is not valid or is not a directory ...
// ... if the path is not readable
if (!is_dir($directory) || !is_readable($directory)) {
return false;
}
// we open the directory
$handle = opendir($directory);
if (!$handle) {
throw new RuntimeException(sprintf('Directory <%s> error', $directory));
}
$skip = array(".", "..");
// and scan through the items inside
while (false !== ($file = readdir($handle))) {
// if the filepointer is not the current directory
// or the parent directory
if (in_array($file, $skip)) {
continue;
}
// we build the new path to delete
$path = $directory . '/' . $file;
// if the new path is a directory
// don't recursively delete symlinks - just remove the actual link
// this is helpful for extensions sym-linked from vendor directory
// previous behaviour would wipe out the files in the vendor directory
if (!is_link($path) && is_dir($path)) {
// we call this function with the new path
$this->recursiveRemoveDirectory($path);
// if the new path is a file
} else {
// we remove the file
unlink($path);
}
}
closedir($handle);
// if the option not empty
if (!$empty) {
return rmdir($directory);
}
// return success
return true;
}