• PHP Help PHP Coding
  • PHP recursive copy function to copy files from many source folders to destinations

The following recursive function copy files from one destination folder to another destination folder. How can i use the same function for copying file from multiple destination folders to destination folders?

if (isset($_POST['submit'])) {

if (!is_dir($dst)) mkdir($dst, 0777);

function recurse_copy($src,$dst) { 
    $dir = opendir($src);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if ( is_dir($src . '/' . $file) ) {
                recurse_copy($src . '/' . $file,$dst . '/' . $file);
            }
            else {
                copy($src . '/' . $file,$dst . '/' . $file);
            }
        }
    }
    closedir($dir);
    //echo "$src";
}
$src = "/home/user/public_html/dir/subdir/source_folder/"; 
$dst = "/home/user/public_html/dir/subdir/destination_folder/"; 
recurse_copy($src,$dst);
}

    Shall I have to? Can't I define multiple sources & destinations folders in a single call?

      Yes, you could (e.g., by checking to see if the arguments are arrays), but it would probably be easier to just call it as many times as needed.

        Hmm, it's simple to check the conditions by arranging multiple source and destination directories in an array .

        but how can I target a single file or folder in source directory to be copied to destination directory in recurse_copy(); function if there are several files or folders are there in the source directory?

        I see no reference about it in PHP copy(); manual.

        Thanks,

          polarexpress;11041379 wrote:

          The following recursive function copy files from one destination folder to another destination folder. How can i use the same function for copying file from multiple destination folders to destination folders?

          if (isset($_POST['submit'])) {
          
          function recurse_copy($src,$dst) { 
              $dir = opendir($src);
              while(false !== ( $file = readdir($dir)) ) {
                  if (( $file != '.' ) && ( $file != '..' )) {
                      if ( is_dir($src . '/' . $file) ) {
                          recurse_copy($src . '/' . $file,$dst . '/' . $file);
                      }
                      else {
                          copy($src . '/' . $file,$dst . '/' . $file);
                      }
                  }
              }
              closedir($dir);
              //echo "$src";
          }
          $src = "/home/user/public_html/dir/subdir/source_folder/"; 
          $dst = "/home/user/public_html/dir/subdir/destination_folder/"; 
          recurse_copy($src,$dst);
          }

          Readers, my code is in unintentional incorrect state. It should be read as:

          if (isset($_POST['submit'])) {
              function recurse_copy($src,$dst) { 
                  $dir = opendir($src);
          		@mkdir($dst); 
                  while(false !== ( $file = readdir($dir)) ) {
                      if (( $file != '.' ) && ( $file != '..' )) {
                          if ( is_dir($src . '/' . $file) ) {
                              recurse_copy($src . '/' . $file,$dst . '/' . $file);
                          }
                          else {
                              copy($src . '/' . $file,$dst . '/' . $file);
                          }
                      }
                  }
                  closedir($dir);
                  //echo "$src";
              }
          if (!is_dir($dst)) mkdir($dst, 0777);
          $src = "/home/user/public_html/dir/subdir/source_folder/"; 
          $dst = "/home/user/public_html/dir/subdir/destination_folder/"; 
          recurse_copy($src,$dst);
          }

          The edited code shall work perfectly based on copy and paste. It also shall create a destination directory on form submit. You only need to replace $scr & $dst variables according to your directory path.

            Write a Reply...