-
-
Save coreyhaines/4677586 to your computer and use it in GitHub Desktop.
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(0 => $this->path); | |
$files = array(); | |
/** Recursive process of Folders. Discovery step for files and directories */ | |
foreach ($objects as $name => $object) { | |
if (is_file($name)) { | |
$files[] = $name; | |
} elseif (is_dir($name)) { | |
$directories[] = $name; | |
} | |
} | |
foreach ($files as $file) { | |
unlink($file); | |
} | |
/** Sort folders in reverse order and delete one at a time */ | |
arsort($directories); | |
foreach ($directories as $directory) { | |
rmdir($directory); | |
} | |
} | |
?> |
:) Thanks. I've written in my fair share of languages, so php's syntax just looks like a mashup of several. :)
I'd prefer an actual recursive call, but I'm not sure how to do functions in php (or whether recursion is frowned upon in php).
Yea, that'd be neat. That's a basic method for a class -- a function.
That array initialization to $this->path is interesting. Have not seen that before, going to have to play with that..
I see, that's the seeded array entry for the initial directory -- you did that to remove the extra rmdir. Makes sense.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So. Very. Pretty. Thank you, kind sir! Loved those notes -- totally agree, very thoughtful, makes perfect sense.
It definitely is much more clear and even re-usable this way. Makes sense to split discover from action.
Not too shabby for "not knowing PHP." Guess it's all that smart Rubyiness.