Last active
September 27, 2015 03:08
-
-
Save leighmcculloch/1202229 to your computer and use it in GitHub Desktop.
PHP Recursive File/Directory Functions
This file contains 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
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