Skip to content

Instantly share code, notes, and snippets.

@GerBawn
Created November 29, 2017 06:23
Show Gist options
  • Save GerBawn/e8fda56f5bacd14b819af34ba24f76e1 to your computer and use it in GitHub Desktop.
Save GerBawn/e8fda56f5bacd14b819af34ba24f76e1 to your computer and use it in GitHub Desktop.
function copyDir($src, $dst)
{
if (!is_dir($src)) {
throw new Exception('src must is a directory.');
}
$absoluteSrcPath = realpath($src);
$absoluteDstPath = realpath($dst);
$dirname = basename($absoluteSrcPath);
$dstDirPath = $absoluteDstPath . '/' . $dirname;
if (!file_exists($dstDirPath)) {
mkdir($dstDirPath);
}
$files = scandir($absoluteSrcPath);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$absoluteFilePath = $absoluteSrcPath . '/' . $file;
if (is_file($absoluteFilePath)) {
copy($absoluteFilePath, $dstDirPath . '/' . $file);
} else {
copyDir($absoluteFilePath, $dstDirPath);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment