Skip to content

Instantly share code, notes, and snippets.

@psolru
Last active March 23, 2020 11:21
Show Gist options
  • Save psolru/3bf66320d74057227467fb5b695f6b4e to your computer and use it in GitHub Desktop.
Save psolru/3bf66320d74057227467fb5b695f6b4e to your computer and use it in GitHub Desktop.
Clear directory recursive in php - low memory consumption (big directories)
<?php
function clearDirectoryRecursive($dir) {
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry == '.' || $entry == '..')
continue;
echo $entry."\n";
if (is_dir($dir.'/'.$entry)) {
clearDirectoryRecursive($dir.'/'.$entry);
rmdir($dir.'/'.$entry);
}
else {
unlink($dir.'/'.$entry);
}
}
closedir($handle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment