Copy Directory

I am listing here the method to copy full Directories with all sub-directories and files.

<?php
	
$source = 'f1'; //folder name		
$target = 'f2'; //folder name , if target folder does not exists, it will be created

//echo 'src->'.$source.'<br/>';
//echo 'tar->'.$target.'<br/>';

			
if(is_dir($source))
{
	
	full_copy($source,$target);
	echo 'Folder copied';
}
else
{
	echo 'Not copied';
}
		
function full_copy( $source, $target )
    {
        if ( is_dir( $source ) )
        {
            @mkdir( $target );
           
            $d = dir( $source );
           
            while ( FALSE !== ( $entry = $d->read() ) )
            {
                if ( $entry == '.' || $entry == '..' )
                {
                    continue;
                }
               
                $Entry = $source . '/' . $entry;           
                if ( is_dir( $Entry ) )
                {
                    full_copy( $Entry, $target . '/' . $entry );
                    continue;
                }
                copy( $Entry, $target . '/' . $entry );
            }
           
            $d->close();
        }else
        {
            copy( $source, $target );
        }
    }

?>

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top