Skip to content

Instantly share code, notes, and snippets.

@cdsalmons
Created September 28, 2015 21:24
Show Gist options
  • Save cdsalmons/5c037a87c4c640c6ea15 to your computer and use it in GitHub Desktop.
Save cdsalmons/5c037a87c4c640c6ea15 to your computer and use it in GitHub Desktop.
PHP: Recursively copy a directory, including symlink support
<?php
// Based on
// http://uk1.php.net/manual/en/function.copy.php#104020
function copy_dir($src, $dst)
{
if (is_link($src)) {
symlink(readlink($src), $dst);
} elseif (is_dir($src)) {
mkdir($dst);
foreach (scandir($src) as $file) {
if ($file != '.' && $file != '..') {
copy_dir("$src/$file", "$dst/$file");
}
}
} elseif (is_file($src)) {
copy($src, $dst);
} else {
echo "WARNING: Cannot copy $src (unknown file type)\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment