Skip to content

Instantly share code, notes, and snippets.

@iksi
Last active September 12, 2017 20:23
Show Gist options
  • Save iksi/51dbb056d24d7c10de289a98e78e64e7 to your computer and use it in GitHub Desktop.
Save iksi/51dbb056d24d7c10de289a98e78e64e7 to your computer and use it in GitHub Desktop.
copy a directory and it's files recursively
<?php
function copyDirectory($sourceDirectory, $destinationDirectory) {
// determine the files that need to be copied
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourceDirectory, FilesystemIterator::SKIP_DOTS)
);
foreach ($files as $file) {
$sourcePath = $file->getPathname();
$destinationPath = $destinationDirectory . DS . $sourcePath;
// create the full path
if (!is_dir(dirname($destinationPath))) {
mkdir(dirname($destinationPath), 0755, true);
}
// finally copy the file
copy($sourcePath, $destinationPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment