Skip to content

Instantly share code, notes, and snippets.

@thbighead
Last active June 22, 2020 14:14
Show Gist options
  • Save thbighead/a3e5c107e3d5f562fbeba9cc7d9f98a0 to your computer and use it in GitHub Desktop.
Save thbighead/a3e5c107e3d5f562fbeba9cc7d9f98a0 to your computer and use it in GitHub Desktop.
Copy a folder and every folders and files inside of it
function recursive_copy($source, $destination)
{
if (is_dir($source) === true) {
$success = true;
if (!file_exists($destination)) {
mkdir($destination, 0777, true);
}
$files = array_diff(scandir($source), ['.', '..']);
foreach ($files as $file) {
$success = recursive_copy(
realpath($source) . DIRECTORY_SEPARATOR . $file,
$destination . DIRECTORY_SEPARATOR . $file
) && $success;
}
return $success;
}
if (is_file($source) === true) {
if (!file_exists($directory_destination = dirname($destination))) {
mkdir($directory_destination, 0777, true);
}
return copy($source, $destination);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment