Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by omar-303 )

copy directory

My first contribution :).... My function to copy all directory’s content, use it as you want but the best way would be to include it in the directory helper because it uses directory_map function or to load both directory helper and whatever helper this function would be in

/**
 * Copy a whole Directory
 *
 * Copy a directory recrusively ( all file and directories inside it )
 *
 * @access    public
 * @param    string    path to source dir
 * @param    string    path to destination dir
 * @return    array
 */    
if(!function_exists('directory_copy'))
{
    
function directory_copy($srcdir$dstdir)
    
{
        
//preparing the paths
        
$srcdir=rtrim($srcdir,'/');
        
$dstdir=rtrim($dstdir,'/');

        
//creating the destenation directory
        
if(!is_dir($dstdir))mkdir($dstdir);
        
        
//Mapping the directory
        
$dir_map=directory_map($srcdir);

        foreach(
$dir_map as $object_key=>$object_value)
        
{
            
if(is_numeric($object_key))
                
copy($srcdir.'/'.$object_value,$dstdir.'/'.$object_value);//This is a File not a directory
            
else
                
directory_copy($srcdir.'/'.$object_key,$dstdir.'/'.$object_key);//this is a dirctory
        
}
    }