InterSet Ltd

PHP Recursive copy files or, Directory Recursively

PHP Recursive copy files

Share Now:

Share on facebook
Share on linkedin
Share on twitter
Share on email
0 0 vote
Article Rating

Bulk Copy Directory Recursively to new folder:

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 custom_copy($src, $dst) {  
  
    // open the source directory 
    $dir = opendir($src);  
  
    // Make the destination directory if not exist 
    @mkdir($dst);  
  
    // Loop through the files in source directory 
    while( $file = readdir($dir) ) {  
  
        if (( $file != '.' ) && ( $file != '..' )) {  
            if ( is_dir($src . '/' . $file) )  
            {  
  
                // Recursively calling custom copy function 
                // for sub directory  
                custom_copy($src . '/' . $file, $dst . '/' . $file);  
  
            }  
            else {  
                copy($src . '/' . $file, $dst . '/' . $file);  
            }  
        }  
    }  
  
    closedir($dir); 
}  

$src = __DIR__.'/folder-2'; 
$dst = __DIR__.'/folder-3';
  
custom_copy($src, $dst);

Special Notes:

For PHP >= 5.3.0 try

PHP magic constants.

__DIR__

And make your path relative.

For PHP < 5.3.0 try

dirname(__FILE__)
0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Get Free Access to InterSet’s 1 GB SSD Hosting with Website Design


Know Your

interset app coming soon

More You Would be interested in . . .

Have you any Question ?
Ask us !

0
Would love your thoughts, please comment.x
()
x