We can use RecursiveDirectoryIterator and RecursiveIteratorIterator to delete directories, subdirectories and files. The subdirectories will be deleted with the parent directory first
- function cleanup_directory($dir) {
- $iter = new RecursiveDirectoryIterator($dir);
- foreach (new RecursiveIteratorIterator($iter, RecursiveIteratorIterator::CHILD_FIRST)
- as $f) {
- if ($f->isDir()) {
- rmdir($f->getPathname());
- } else {
- unlink($f->getPathname());
- }
- }
- rmdir($dir);
- }
-
- cleanup_directory('c:\wamp\junk');
- ?>
Copy code
|