Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Last active June 15, 2018 20:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AmyStephen/4677117 to your computer and use it in GitHub Desktop.
Save AmyStephen/4677117 to your computer and use it in GitHub Desktop.
Delete all files and folders in a directory
<?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);
}
?>
@coreyhaines
Copy link

There we go. Got github to let me fork it. Here are my thoughts in the actual script:
https://gist.github.com/4677586

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment