How to Copy a Directory

Discussion in 'PHP' started by belthazar, Apr 24, 2006.

  1. #1
    Anyone has an idea on how to copy a whole folder. something like this

    copyDir($source,$destination);
     
    belthazar, Apr 24, 2006 IP
  2. belthazar

    belthazar Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I Found a script.. Problem solved!

    //Copy a directory!
    define('loc1', '/var/www/html/xuonline');
    //you can assign $username with a $_POST value. Think of the possibilities!
    $username = "kim";
    $source = "/xuBlog/src/";
    $dest = "/xuBlog/$username/";
    mkdir(loc1.$dest);
    dircpy($source,$dest);

    function dircpy($source, $dest, $overwrite = false)
    {
    if($handle = opendir(loc1 . $source))// if the folder exploration is sucsessful, continue
    {
    while(false !== ($file = readdir($handle)))// as long as storing the next file to $file is successful, continue
    {
    if($file != '.' && $file != '..')
    {
    $path = $source . '/' . $file;

    if(is_file(loc1 . $path))
    {
    if(!is_file(loc1 . $dest . '/' . $file) || $overwrite)
    if(!@copy(loc1 . $path, loc1 . $dest . '/' . $file))
    {
    echo 'ERROR';
    }
    }

    elseif(is_dir(loc1 . $path))
    {
    if(!is_dir(loc1 . $dest . '/' . $file))
    mkdir(loc1 . $dest . '/' . $file); // make subdirectory before subdirectory is copied
    dircpy($path, $dest . '/' . $file, $overwrite); //recurse!
    }
    }
    }
    closedir($handle);
    }
    } // end of dircpy()
     
    belthazar, Apr 24, 2006 IP
  3. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #3
    Congrats. And the code is well written.
     
    Lordo, Apr 25, 2006 IP