PHP Code Snippets

Bulk copy directory recursively
David Carr

This function is used to copy a folder and all its contents and sub folders, its params are as follows:

1) The source folder path
2) The destination path (should not exist)

function recurseCopy($src, $dst)
{
    $dir = opendir($src);
    mkdir($dst);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                $this->recurseCopy($src . '/' . $file, $dst . '/' . $file);
            } else {
                copy($src . '/' . $file,$dst . '/' . $file);
            }
        }
    }
    closedir($dir);
}

 



Copyright © 2011 - 2017 created by David Carr Web Developer. All code MIT license. All rights reserved. Built using Simple MVC Framework