Last active
June 22, 2020 14:14
-
-
Save thbighead/a3e5c107e3d5f562fbeba9cc7d9f98a0 to your computer and use it in GitHub Desktop.
Copy a folder and every folders and files inside of it
This file contains hidden or 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_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