Created
December 3, 2020 17:31
-
-
Save windbridges/019dd8767b7f1f9ac83e823f74fed876 to your computer and use it in GitHub Desktop.
Recursive directory cleanup
This file contains 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 | |
use FilesystemIterator; | |
use RecursiveDirectoryIterator; | |
use RecursiveIteratorIterator; | |
trait WithCleanupDirectory | |
{ | |
public function cleanupDirectory(string $path, bool $deleteDir = false) | |
{ | |
$di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); | |
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST); | |
foreach ($ri as $file) { | |
$file->isDir() ? rmdir($file) : unlink($file); | |
} | |
$deleteDir && rmdir($path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment