Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ClasslessAndFree/2470206 to your computer and use it in GitHub Desktop.
Save ClasslessAndFree/2470206 to your computer and use it in GitHub Desktop.
PHP: Recursive Directory Functions
function recursive_delete($dir) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
$path = $file->__toString();
if($file->isDir()) {
rmdir($path);
} else {
unlink($path);
}
}
}
function recursive_copy($dir, $dest) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
$path = $file->__toString();
$relpath = substr($path, strlen($dir));
if($file->isDir()) {
mkdir($dest.$relpath);
} else {
copy($path, $dest.$relpath);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment