Skip to content

Instantly share code, notes, and snippets.

@SDKiller
Created September 12, 2014 17:11
Show Gist options
  • Save SDKiller/059f2729f51d18979bf3 to your computer and use it in GitHub Desktop.
Save SDKiller/059f2729f51d18979bf3 to your computer and use it in GitHub Desktop.
FileHelper override
public static function removeDirectory($dir, $options = [])
{
if (!is_dir($dir)) {
echo 'Path is not a directory or not exists: ' . $dir . PHP_EOL;
return;
}
if (!is_link($dir) || isset($options['traverseSymlinks']) && $options['traverseSymlinks']) {
if (!($handle = opendir($dir))) {
echo 'Failed to open: ' . $dir . PHP_EOL;
return;
}
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($path)) {
static::removeDirectory($path, $options);
} else {
if (!unlink($path)) {
echo 'Failed to delete file: ' . $dir . PHP_EOL;
}
}
}
closedir($handle);
}
if (is_link($dir)) {
if (!unlink($dir)) {
echo 'Failed to delete file: ' . $dir . PHP_EOL;
}
} else {
if (!rmdir($dir)) {
echo 'Failed to delete folder: ' . $dir . PHP_EOL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment