开发者

Recursive function for copy of multilevel folder is not working

开发者 https://www.devze.com 2022-12-30 00:45 出处:网络
Recursive function for copy of multilevel folder is not working. I have a code to copy all the mulitilevel folder to new folder.

Recursive function for copy of multilevel folder is not working.

I have a code to copy all the mulitilevel folder to new folder.

But in between I feel there is problem of proper path recognition, see the code below..

<?php
$source = '/var/www/html/pranav_test';
$destination = '/var/www/html/parth';

copy_recursive_dirs($source, $destination);
function copy_recursive_dirs($dirsource, $dirdest)
{ 
  // recursive function to copy
 // all subdirectories and contents:
 if(is_dir($dirsource))
 {
  $dir_handle=opendir($dirsource);



 }
 if(!is_dir($dirdest))
 {
    mkdir($dirdest, 0777);
 }
 while($file=readdir($dir_handle))
 {/*echo "<pre>";
  print_r($file);*/
     if($file!="." && $file!="..")
开发者_如何转开发     {
         if(!is_dir($dirsource.DIRECTORY_SEPARATOR.$file)) 
         {
           copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$dirsource.DIRECTORY_SEPARATOR.$file);
         }
         else{
            copy_recursive_dirs($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest);
         }
     }
  }
 closedir($dir_handle);
 return true;
}

?>

from the above code the if loop has a copy function as per requirement, but the path applied for destination here is not proper, I have tried with basename function as well.. but it didnt got the expected result.. below is the if loop i am talking about with comment describing the output...

 if(!is_dir($dirsource.DIRECTORY_SEPARATOR.$file)) 
    {
      $basefile = basename($dirsource.DIRECTORY_SEPARATOR.$file);//it gives the file name
     echo "Pranav<br>".$dirdest.DIRECTORY_SEPARATOR.$dirsource.DIRECTORY_SEPARATOR.$file;//it outputs for example "/var/www/html/parth//var/www/html/pranav_test/media/system/js/caption.js" which is not correct..
    copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$dirsource.DIRECTORY_SEPARATOR.$file);
    }  

as shown above the i cannot get the files and folders copied to expected path.. please guide me to place proper path in the function....


I see some strange things in the code about the destination path of dir and files, try with this code (not tested):

<?php
$source = '/var/www/html/pranav_test';
$destination = '/var/www/html/parth';

copy_recursive_dirs($source, $destination);
function copy_recursive_dirs($dirsource, $dirdest)
{ 
  // recursive function to copy
 // all subdirectories and contents:
 if(is_dir($dirsource))
 {
  $dir_handle=opendir($dirsource);



 }
 if(!is_dir($dirdest))
 {
    mkdir($dirdest, 0777);
 }
 while($file=readdir($dir_handle))
 {/*echo "<pre>";
  print_r($file);*/
     if($file!="." && $file!="..")
     {
         if(!is_dir($dirsource.DIRECTORY_SEPARATOR.$file)) 
         {
            //Copy the file at the same level in the destination folder
            copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);
         }
         else{
            //Copy the dir at the same lavel in the destination folder
            copy_recursive_dirs($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);
         }
     }
  }
 closedir($dir_handle);
 return true;
}

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消