Skip to content

Instantly share code, notes, and snippets.

@rachelbaker
Created September 2, 2012 22:30
Show Gist options
  • Save rachelbaker/3605210 to your computer and use it in GitHub Desktop.
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
/**
* 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