Last active
March 23, 2020 11:21
-
-
Save psolru/3bf66320d74057227467fb5b695f6b4e to your computer and use it in GitHub Desktop.
Clear directory recursive in php - low memory consumption (big directories)
This file contains hidden or 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 | |
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