Aidan's PHP Repository

A repository for PHP functions and classes ...

function.copyr.php

Copy a file, or recursively copy a folder and its contents

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.0.1
  • Link: http://aidanlister.com/repos/v/function.copyr.php
  • Return: bool Returns TRUE on success, FALSE on failure
  • Views: 37081
  • Downloads: 2775

Source

Download this script <?php
/**
 * Copy a file, or recursively copy a folder and its contents
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.1
 * @link        http://aidanlister.com/repos/v/function.copyr.php
 * @param       string   $source    Source path
 * @param       string   $dest      Destination path
 * @return      bool     Returns TRUE on success, FALSE on failure
 */
function copyr($source, $dest)
{
    // Simple copy for a file
    if (is_file($source)) {
        return copy($source, $dest);
    }
 
    // Make destination directory
    if (!is_dir($dest)) {
        mkdir($dest);
    }
    
    // If the source is a symlink
    if (is_link($source)) {
        $link_dest = readlink($source);
        return symlink($link_dest, $dest);
    }
 
    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }
 
        // Deep copy directories
        if ($dest !== "$source/$entry") {
            copyr("$source/$entry", "$dest/$entry");
        }
    }
 
    // Clean up
    $dir->close();
    return true;
}
 
?>

Comments

August 31st, 2006
Wow, This is my FIRST work script !
August 3rd, 2006
Excellent script it works perfectly. If you are looking to try to make the exec (cp) work, this is easier for real
May 28th, 2006
really great. Thank you for all you guys!
May 16th, 2006
thanks
February 1st, 2006
Should $source/$entry be $source . DIRECTORY_SEPARATOR . $entry to handle windows paths? [Editor's Note: No, PHP automatically handles the conversion on Windows.]
January 29th, 2006
Thanx,simple but powerful algorithm
November 26th, 2005
I spent hours trying to write something similar. thanks.
July 29th, 2005
The stack array is damned cool. That's a technique I've not seen before.
July 5th, 2005
This is a function that does the same but uses a stack array to make it work faster. Using recursion is (nearly?) always slower since it takes up extra resources for every instance of the function and all the set variables in the function instances. Comments are always welcome :) http://pastebin.com/775560
June 29th, 2005
Very nice! Thank's
June 24th, 2005
thank's
June 2nd, 2005
thanks for the great function. I've addapted it to copy only files from certain types, passing an optional array as a third parameter. The result is here, and I hope it will be useful for others: http://smalltalking.com/wiki/php:copyr
May 17th, 2005
Hi and thanx
April 26th, 2005
thanks... i was so certain i'd break this thing in a matther of minutes but it seems to work pretty nicely!
March 23rd, 2005
Good little script Aidan. Strange that there is no native PHP function.
March 14th, 2005
Thanks for useful example
January 6th, 2005
Excellent script, I hate thinking recursively. This way may allow to copy the permissions over from the source file to the destination: Then apply the permissions in $perms to the destination file after its been copied: I've had numerous problems changing permissions in my php scripts. If you're running PHP on a linux box then try editing the httpd.conf file and changing the user and group apache runs as. Then make that user a member of the group the files that you want to chmod belongs to. Then restart apache. Hope this helps.
January 1st, 2005
very nice!
December 31st, 2004
hehello; good script ! thanks
December 31st, 2004
Thanks dude! You write beautiful recursive code!
December 4th, 2004
thanks alot for this one. was too lazy to get my brain thinking about a recursive function for it. needed this one quite often. great that someone wrote in down :) regards, chill
December 3rd, 2004
thanks a squillion for this function! I hope you can enjoy the music from my site as a way thanks: futureboogiedotcom
November 30th, 2004
Hello, First Thanks for the nice code. I have something to say on your algorithm. Here you can do simply this: if ($dest !== "$source/$entry") { copyr("$source/$entry", "$dest/$entry"); } If it is a file you do test it in the beginning of your function. If it is the destination you just have to continue. Hope I am useful! Regards, Dimitar Peev [Editor's note: Good pick up, thanks]
November 5th, 2004
Thanx for the perfect COPYR func
November 5th, 2004
simple, but nice THX :)
November 5th, 2004
Awesome =)
November 5th, 2004
thanks for the function! saved me some brainwaves during a hard programming evening...