用php比较两个文件夹的异同

WBOY
Freigeben: 2016-07-25 09:11:16
Original
1066 Leute haben es durchsucht
要求: 只能使用命令行,比较两个文件夹的不同,包括文件的差异。
思考: 虽然linux下有diff。。。。还是用php吧,代码改的方便,速度也很快,以下排除了.svn目录的比较 文件要比较md5校验和
思路: 1)把第一路径作为标准路径,列出第1个路径中有的,第2个路径中没有的文件或文件夹,或者是不同的文件。 2)然后,列出第2个路径中有的,第1个路径中却不存在的文件和文件夹。
调用示例: php compare_folder.php /home/temp/2 /home/temp/55 //转载自JAVAEYE
  1. /**
  2. * 工具文件
  3. * 目的在于递归比较两个文件夹
  4. *
  5. * 调用示例
  6. * php compare_folder.php /home/temp/2 /home/temp/55
  7. *
  8. */
  9. //参数确定
  10. if (count($argv) > 1 )
  11. $dir1 = del_postfix($argv[1]);
  12. else
  13. $dir1 = '/';
  14. if (count($argv) > 2 )
  15. $dir2 = del_postfix($argv[2]);
  16. else
  17. $dir2 = '/';
  18. //检查第一个路径有,后者没有或错误的方法。
  19. process_compare($dir1, $dir2, 0);
  20. echo "===========================================================\n";
  21. //检查第2个路径的多余文件夹或文件
  22. process_compare($dir2 , $dir1, 1);
  23. echo "all OK\n";
  24. /**
  25. * 去除路径末尾的/,并确保是绝对路径
  26. *
  27. * @param unknown_type $dir
  28. * @return unknown
  29. */
  30. function del_postfix($dir)
  31. {
  32. if (!preg_match('#^/#', $dir)) {
  33. throw new Exception('参数必须是绝对路径');
  34. }
  35. $dir = preg_replace('#/$#', '', $dir);
  36. return $dir;
  37. }
  38. /**
  39. * 公用函数,会调用一个递归方法实现比较
  40. *
  41. * @param string $dir1 作为标准的路径
  42. * @param string $dir2 对比用的路径
  43. * @param int $only_check_has 为1表示不比较文件差异,为0表示还要比较文件的md5校验和
  44. */
  45. function process_compare($dir1, $dir2, $only_check_has){
  46. compare_file_folder($dir1, $dir1, $dir2, $only_check_has);
  47. }
  48. /**
  49. * 真实的函数,私有函数
  50. *
  51. * @param string $dir1 路径1,是标准
  52. * @param string $base_dir1 不变的参数路径2
  53. * @param string $base_dir2 不变的待比较的路径2
  54. * @param int $only_check_has 为1表示不比较文件差异,为0表示还要比较文件的md5校验和
  55. *
  56. */
  57. function compare_file_folder($dir1, $base_dir1, $base_dir2, $only_check_has=0){
  58. if (is_dir($dir1)) {
  59. $handle = dir($dir1);
  60. if ($dh = opendir($dir1)) {
  61. while ($entry = $handle->read()) {
  62. if (($entry != ".") && ($entry != "..") && ($entry != ".svn")){
  63. $new = $dir1."/".$entry;
  64. //echo 'compare: ' . $new . "\n";
  65. $other = preg_replace('#^'. $base_dir1 .'#' , $base_dir2, $new);
  66. if(is_dir($new)) {
  67. //比较
  68. if (!is_dir($other)) {
  69. echo '!!not found direction: '. $other. ' (' . $new .")\n";
  70. }
  71. compare_file_folder($new, $base_dir1,$base_dir2, $only_check_has) ;
  72. } else { //如果1是文件,则2也应该是文件
  73. if (!is_file($other)) {
  74. echo '!!not found file: '. $other. ' ('.$new .")\n";
  75. }elseif ($only_check_has ==0 && ( md5_file($other) != md5_file($new) ) ){
  76. echo '!!file md5 error: '. $other. ' ('.$new .")\n";
  77. }
  78. }
  79. }
  80. }
  81. closedir($dh);
  82. }
  83. }
  84. }
  85. ?>
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!