Created
September 2, 2012 22:30
-
-
Save rachelbaker/3605210 to your computer and use it in GitHub Desktop.
PHP method to copy a single file or recursively copy an entire directory
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
/** | |
* Copy a single file or recursively copy a directory along with all contents | |
* @param string $source Source path | |
* @param string $dest Destination path | |
* @return bool Returns TRUE on success, FALSE on failure | |
*/ | |
function copyr($source, $dest) | |
{ | |
// if only one file | |
if (is_file($source)) { | |
return copy($source, $dest); | |
} | |
if (!is_dir($dest)) { | |
mkdir($dest); | |
} | |
// Loop through the folder | |
$dir = dir($source); | |
while (false !== $entry = $dir->read()) { | |
if ($entry == '.' || $entry == '..') { | |
continue; | |
} | |
// Deep copy directories | |
if ($dest !== "$source/$entry") { | |
$this->copyr("$source/$entry", "$dest/$entry"); | |
} | |
} | |
$dir->close(); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment