Zend - The PHP Company




Files and Directories

Add Code


XCOPY in PHP  

Type: code fragment
Added by: imranlink
Entered: 08/08/2004
Last modified: 08/12/2003
Rating: - (fewer than 3 votes)
Views: 4883
This function copy all the files and directories (including all sub directores) from target to source like XCOPY command with simplest way.


<?php
/*
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   File:                    xcopy.php
'   Description:            Xcopy in PHP (For Taking Backup of your Web site)
'   Written by:             Imran Khalid imranlink@hotmail.com
'   Date Written:           June 14, 2004
'   Platform:               Windows 2000 / IIS / PHP
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
*/
    
$dirPathSource "c:/inetpub/wwwroot/mysite/home/"// change to your Web Home Directory.
    
$dirPathTarget "c:/inetpub/wwwroot/mysite/backup/"// edit the root path according to your target Directory.
    
xcopy($dirPathSource,$dirPathTarget);
    
function 
xcopy($dirPathSource,$dirPathTarget)
{   
    if (
$handle = @opendir($dirPathSource))
    {
        while (
false !== ($dir readdir($handle)))
        {
            if (
$dir != '.' && $dir != '..')
            {
                
$pos =  strpos $dirPathSource"/");
                
$newdir substr($dirPathSource$pos);
                
$pos =  strpos $newdir"/");
                
$newdir substr($newdir$pos+1);
                
$pos =  strpos $newdir"/");
                
$fromDir $dirPathSource $dir;
                   
$toDir $dirPathTarget $dir;
                if (
is_dir($fromDir))
                {
                    
$mkSuccess mkdir($toDir);
                    
xcopy($fromDir '/'$toDir '/');
                }
                else
                {
                    
copy($fromDir,$toDir);
                    echo 
"rn<br><br> From: $fromDir rn<br> TO: $toDir";
                }
            } 
// if not . .. close
        
// while close
        
closedir($handle);
    } 
// if handle close
}
////////////////////////////////////////////////////////////////////////////////////////////////
?>


Usage Example




Rate This Script





Search



This Category All Categories