Skip to content

Instantly share code, notes, and snippets.

@leighmcculloch
Last active September 27, 2015 03:08
Show Gist options
  • Save leighmcculloch/1202229 to your computer and use it in GitHub Desktop.
Save leighmcculloch/1202229 to your computer and use it in GitHub Desktop.
PHP Recursive File/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