Zend - The PHP Company




Files and Directories

Add Code


XCOPY in PHP  

Type: code fragment
Added by: php_guy
Entered: 23/06/2003
Last modified: 09/12/2002
Rating: - (fewer than 3 votes)
Views: 5350
This function copy all the files and directories (including all sub directores) from target to source like XCOPY command.


<?php
/*
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   File:                    xcopy.php
'
'   Description:            Xcopy in PHP
'   Written by:             Imran Khalid imranlink@hotmail.com
'
'   Languages:              PHP
'
'   Date Written:           June 14, 2003
'
'   Date Modify:            June 14, 2003
'
'   Platform:               Linux 
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
*/
    
$basedir "techbuys/";  // Please set your basedir.
    
$txtFolderName "past_inside_me"// Folder name where u want to copy the files.
    
$dirPathTarget "c:/inetpub/wwwroot/"$txtFolderName// edit the root path according to your server OS.
    
$dirPathSource "c:/inetpub/wwwroot/shoppingclone/techbuys"// change according to your requirements.
    
xcopy($basedir,$txtFolderName); 
    
/////////////////////////////////////////////////////////////////////////////////////////////////////
function xcopy($basedir,$txtFolderName){ 
    
    if (
$handle = @opendir($basedir)) { 
        while (
false !== ($dir readdir($handle))){ 
            if (
$dir != '.' && $dir != '..'){ 
                
$pos =  strpos $basedir"/");
                
$newdir substr($basedir$pos); 
                
$pos =  strpos $newdir"/");
                
$newdir substr($newdir$pos+1); 
                
$pos =  strpos $newdir"/");
                
$dirPathTarget "c:/inetpub/wwwroot/"$txtFolderName ."/"$newdir ."/"$dir// edit the root path according to your server OS.
                
$dirPathSource "c:/inetpub/wwwroot/shoppingclone/techbuys/"$newdir ."/"$dir// edit the root path according to your server OS.
                
$dirPathTarget  str_replace("//","/",$dirPathTarget);
                
$dirPathSource  str_replace("//","/",$dirPathSource);
    
                if (
is_dir($basedir."/".$dir)){ 
                    
$mkSuccess mkdir($dirPathTarget);
                    
xcopy($basedir."/".$dir,$txtFolderName); 
                } 
                else{
                    echo 
"$dirPathTarget<br>"
                    
copy($dirPathSource$dirPathTarget);
                }
            } 
        } 
        
closedir($handle); 
    } 

////////////////////////////////////////////////////////////////////////////////////////////////
?>


Usage Example




Rate This Script





Search



This Category All Categories