Home >Backend Development >PHP Tutorial >Compare the similarities and differences between two folders using php

Compare the similarities and differences between two folders using php

WBOY
WBOYOriginal
2016-07-25 09:11:161137browse
Requirements: You can only use the command line to compare the differences between two folders, including file differences.
Thinking: Although there is diff under linux. . . . Let’s use PHP. Code modification is easy and fast. The comparison of .svn directories is excluded below The file needs to be compared with md5 checksum
Thinking: 1) Use the first path as the standard path and list the files or folders that are in the first path but not in the second path, or are different files. 2) Then, list the files and folders that exist in the second path but do not exist in the first path.
Call example: php compare_folder.php /home/temp/2 /home/temp/55 //Reprinted from JAVAEYE
  1. /**
  2. * Tool file
  3. * The purpose is to recursively compare two folders
  4. *
  5. * Calling example
  6. * php compare_folder.php /home/temp/2 /home/temp/55
  7. *
  8. */
  9. //Parameter determination
  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. //Check the first One path has it, the latter doesn't or has the wrong method.
  19. process_compare($dir1, $dir2, 0);
  20. echo "====================================== ========================n";
  21. //Check the second path for extra folders or files
  22. process_compare($dir2, $dir1 , 1);
  23. echo "all OKn";
  24. /**
  25. * Remove the / at the end of the path and make sure it is an absolute path
  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('The parameter must be an absolute path');
  34. }
  35. $dir = preg_replace('#/$#', '', $dir);
  36. return $dir;
  37. }
  38. /**
  39. * Public function, which will call a recursive method to implement comparison
  40. *
  41. * @param string $dir1 as the standard path
  42. * @param string $dir2 The path used for comparison
  43. * @param int $only_check_has 1 means no file differences will be compared , a value of 0 means that the md5 checksum of the file must also be compared
  44. */
  45. function process_compare($dir1, $dir2, $only_check_has){
  46. compare_file_folder($dir1, $dir1, $dir2, $only_check_has);
  47. }
  48. /**
  49. * Real function, private function
  50. *
  51. * @param string $dir1 Path 1, which is the standard
  52. * @param string $base_dir1 Unchanged parameter path 2
  53. * @param string $base_dir2 Unchanged path 2 to be compared
  54. * @param int $only_check_has If it is 1, it means not to compare the file differences. If it is 0, it means the md5 checksum of the file should also be compared.
  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. //Compare
  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 { //If 1 is a file, then 2 should also be a file
  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. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn