我只需要复制 PHP 中选定的文件和文件夹

发布于 2024-09-02 13:33:26 字数 2890 浏览 1 评论 0原文

我在 WINDOWS 操作系统和 PHP 脚本中使用以下代码,其中最初我获取两个文件夹结构的差异,然后需要将输出复制到其他文件夹。这是下面的代码。

$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($dirsource, $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;
}

每当我执行脚本时,我都会得到差异输出,但没有按照代码在第二个文件夹上得到其他副本...请帮助我纠正...

更新 我只想将差异复制到 其他文件夹,如果有的话 请帮助我......

更新: 我收到这些错误,

(!) 警告: opendir(var/www/html/copy2/新文件夹 (2)) [function.opendir]: 失败 打开目录: 没有这样的文件或目录 /var/www/html/pranav_test/syncss.php 第96行

(!) 警告:mkdir() [function.mkdir]:没有这样的文件或 目录在 /var/www/html/pranav_test/syncss.php 第99行

( ! ) 警告:readdir():提供 参数不是有效的目录 资源在 /var/www/html/pranav_test/syncss.php 第 104 行

( ! ) 警告:closedir():已提供 参数不是有效的目录 资源在 /var/www/html/pranav_test/syncss.php 第 122 行

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($dirsource, $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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-09-09 13:33:26

不要试图一次完成所有事情。
将您的任务分成更小的块。
仅当您完成并测试了上一部分后才可以进入下一部分。

首先,学习找出两个数组之间的差异:
在脚本中硬编码 2 个略有不同的数组,并使用 array_diff() 函数

接下来,学习从目录中读取文件。不是递归,只是一个。
以文件名数组结束。
使用 print_r() 进行测试

现在您可以尝试将 2 个目录读入数组并进行比较。
如果仍然不行,调试:打印出数组的内容,与你的眼睛进行比较。使用直接、清晰的输入数据寻求有关此特定问题的帮助。
最终会出现一系列差异,打印在屏幕上以确保它包含实际数据。

好吧,现在你可以进行复制部分了。相同的技术。

然后你就可以使用递归目录。相同的技术 - 在每个阶段进行双重检查和测试。打印出尽可能多的调试信息。

打开错误报告并确保您可以看到发生的每个错误(只需故意犯一个错误即可查看)。 每个文件系统操作失败时都会抛出错误。所以,你就会看到原因。并调试文件系统操作。从条件内部打印出来,看看它是否被执行过。打印出变量内容、函数返回值。

有没有想过,这个 $dirsource.DIRECTORY_SEPARATOR.$file 行的实际值是多少?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文