开发者

I need to copy only selected files and folders in PHP

I am using the following code in WINDOWS OS and PHP script, in which initially i am taking the difference of two folder structure and then the out put needs to be copied to other folder. here is the code below..

$source = '/var/www/html/copy1';
$mirror = '/var/www/html/copy2';
function scan_dir_recursive($dir, $rel = null) {

  $all_paths = array();
  $new_paths = scandir($dir);

  foreach ($new_paths as $path) {

    if ($path == '.' || $path == '..') {
      continue;
    }

    if ($rel === null) {
        $path_with_rel = $path;
    } else {
        $path_with_rel = $rel . DIRECTORY_SEPARATOR . $path;
    }

    $full_path = $dir . DIRECTORY_SEPARATOR . $path;
    $all_paths[] = $path_with_rel;

    if (is_dir($full_path)) {
      $all_paths = array_merge(
        $all_paths, scan_dir_recursive($full_path, $path_with_rel)
      );
    }

  }

  return $all_paths;

}
$diff_paths = array_diff(
    scan_dir_recursive($mirror),
    scan_dir_recursive($source)
);


/*$diff_path = array_diff($mirror,$original);*/
echo "<pre>Difference ";print_r($diff_paths);
    Difference of Folders Array
(
    [4] => New Folder (2)
    [5] => New Folder (2)/New Folder
    [6] => New Folder (2)/New Folder/New Folder
    [7] => New Folder (2)/New Folder/New Folder/New Text Document (2).txt
    [8] => New Folder (2)/New Folder/New Folder/New Text Document.txt
)

foreach($diff_paths as $path)
{
    echo $source1 = "var/www/html/copy2/".$path;
    echo "<br>";
    $des = "var/www/html/copy1/".$path;
    copy_recursive_dirs($source1, $des);
}

function copy_recursive_dirs($dir开发者_如何转开发source, $dirdest)
{ 
   $dir_handle=opendir($dirsource);

    mkdir($dirdest,0777);

 while(false!==($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_recursive_dirs($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);

         }
         else{
            //Copy the dir at the same lavel in the destination folder
             copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);

         }
     }
  }
 closedir($dir_handle);
 return true;
}

Whenever I execute the script I get the difference output but do not get the other copy on second folder as per code... Pls help me in rectifying...

UPDATE I just want to copy the difference to other folder, if theere is any other way pls help me....

UPDATE: I am getting these errors,

( ! ) Warning: opendir(var/www/html/copy2/New Folder (2)) [function.opendir]: failed to open dir: No such file or directory in /var/www/html/pranav_test/syncss.php on line 96

( ! ) Warning: mkdir() [function.mkdir]: No such file or directory in /var/www/html/pranav_test/syncss.php on line 99

( ! ) Warning: readdir(): supplied argument is not a valid Directory resource in /var/www/html/pranav_test/syncss.php on line 104

( ! ) Warning: closedir(): supplied argument is not a valid Directory resource in /var/www/html/pranav_test/syncss.php on line 122


Do not try to do everything at once.
Separate your task into smaller chunks.
And pass to the next one only if you had finishad and tested previous part.

First, learn to find the difference between 2 arrays:
Hardcode 2 slightly different arrays in your script and play with array_diff() function

Next, learn to read files from directory. Not recursive, just one.
End up with array of filenames.
Test it with print_r()

Now you can try to read 2 directories into arrays and compare it.
If it still not working, debug it: print out content of arrays, compare it with your eyes. Ask for help on SO for this particular and certain problem with straight and clear input data. end up with an array of differences, printed on the screen to be sure it contains actual data.

Well, now you can go for the copy part. Same technique.

And then you can go for recursive directories. Same technique - double-checking and testing on every stage. Print out as much debugging information as possible.

Turn on error reporting and ensure you can see every error occurred (just make an intentional one and see). every filesystem operation will throw an error on failure. So, you will see the reason. And debug filesystem operations as well. Print out from inside the conditions to see if it ever got executed. Print out variable contents, function return values.

Ever got curious, what is this $dirsource.DIRECTORY_SEPARATOR.$file line's actual value?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜