Recursive function for copy of multilevel folder is not working.

Posted by OM The Eternity on Stack Overflow See other posts from Stack Overflow or by OM The Eternity
Published on 2010-05-07T09:23:24Z Indexed on 2010/05/07 9:28 UTC
Read the original article Hit count: 209

Filed under:
|
|

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....

© Stack Overflow or respective owner

Related posts about php

Related posts about copy