Skip to content

Instantly share code, notes, and snippets.

@rotexdegba
Created January 28, 2023 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rotexdegba/832c68a1b14b3d7a35771ed668b4a114 to your computer and use it in GitHub Desktop.
Save rotexdegba/832c68a1b14b3d7a35771ed668b4a114 to your computer and use it in GitHub Desktop.
Recursive Directory Deletion PHP
<?php
function rmdirRecursive(string $dir) {
$iter = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($iter, RecursiveIteratorIterator::CHILD_FIRST);
foreach($it as $file) {
if ($file->isDir()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}
}
rmdir($dir);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment