Last active
June 15, 2018 20:05
-
-
Save AmyStephen/4677117 to your computer and use it in GitHub Desktop.
Delete all files and folders in a directory
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 | |
$this->path = BASE_FOLDER . '/x/y/z'; | |
if (file_exists($this->path)) { | |
$objects = new RecursiveIteratorIterator ( | |
new RecursiveDirectoryIterator($this->path), | |
RecursiveIteratorIterator::SELF_FIRST); | |
$directories = array(); | |
$i = 0; | |
/** Recursive process of Folders */ | |
foreach ($objects as $name => $object) { | |
/** Remove files as found */ | |
if (is_file($name)) { | |
unlink($name); | |
/** Hold folders until end */ | |
} elseif (is_dir($name)) { | |
$directories[$i++] = $name; | |
} | |
} | |
/** Sort folders in reverse order and delete one at a time */ | |
arsort($directories); | |
foreach ($directories as $name) { | |
rmdir($name); | |
} | |
/** Remove the seed path folder */ | |
rmdir($this->path); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There we go. Got github to let me fork it. Here are my thoughts in the actual script:
https://gist.github.com/4677586