Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created May 30, 2017 01:08
Show Gist options
  • Save chwnam/124a28e8169858cf0efacfedbfeb9e0b to your computer and use it in GitHub Desktop.
Save chwnam/124a28e8169858cf0efacfedbfeb9e0b to your computer and use it in GitHub Desktop.
Recursively remove a directory
<?php
header('Content-Type: text/plain');
function rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
$fullPath = $dir . "/" . $object;
if (is_dir($fullPath)) {
echo('DIR:' . $fullPath . "\n");
rrmdir($fullPath);
} else {
echo('FILE:' . $fullPath . "\n");
unlink($fullPath);
}
}
}
rmdir($dir);
} else {
echo "'$dir' is not a directory!\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment