Compare two folders with PHP

Source: Internet
Author: User
Requirements: You can use only the command line to compare two folders, including file differences.
thinking: although Linux has diff .... or use PHP bar, code to change the convenience, speed is also very fast, the following exclusions. SVN directory comparison file to compare the MD5 checksum
Ideas: 1) Take the first path as the standard path, list the 1th path of the file or folder that is not in the 2nd path, or a different file. 2) Then, list the files and folders in the 2nd path that do not exist in the 1th path.
Invocation Example: php compare_folder.php/home/temp/2/home/temp/55Reprinted from Javaeye
  1. /**
  2. * Tool Files
  3. * Purpose is to compare two folders recursively
  4. *
  5. * Call Example
  6. * PHP COMPARE_FOLDER.PHP/HOME/TEMP/2/HOME/TEMP/55
  7. *
  8. */
  9. Parameter determination
  10. if (count ($argv) > 1)
  11. $dir 1 = Del_postfix ($argv [1]);
  12. Else
  13. $dir 1 = '/';
  14. if (count ($ARGV) > 2)
  15. $dir 2 = Del_postfix ($argv [2]);
  16. Else
  17. $dir 2 = '/';
  18. Check that the first path has a method that does not have the latter or the wrong one.
  19. Process_compare ($dir 1, $dir 2, 0);
  20. echo "===========================================================\n";
  21. Check for extra folders or files on route 2nd
  22. Process_compare ($dir 2, $dir 1, 1);
  23. echo "All ok\n";
  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 (' parameter must be absolute path ');
  34. }
  35. $dir = preg_replace (' #/$# ', ' ', $dir);
  36. return $dir;
  37. }
  38. /**
  39. * Common function, a recursive method is called to implement the comparison
  40. *
  41. * @param string $dir 1 as the standard path
  42. * @param string $dir 2 comparison path
  43. * @param int $only _check_has to 1 to not compare file differences, 0 to compare file MD5 checksum
  44. */
  45. function Process_compare ($dir 1, $dir 2, $only _check_has) {
  46. Compare_file_folder ($dir 1, $dir 1, $dir 2, $only _check_has);
  47. }
  48. /**
  49. * Real functions, private functions
  50. *
  51. * @param string $dir 1 path 1, is the standard
  52. * @param string $base _dir1 unchanged parameter path 2
  53. * @param string $base _dir2 unchanged path to be compared 2
  54. * @param int $only _check_has to 1 to not compare file differences, 0 to compare file MD5 checksum
  55. *
  56. */
  57. function Compare_file_folder ($dir 1, $base _dir1, $base _dir2, $only _check_has=0) {
  58. if (Is_dir ($dir 1)) {
  59. $handle = Dir ($dir 1);
  60. if ($dh = Opendir ($dir 1)) {
  61. while ($entry = $handle->read ()) {
  62. if ($entry! = ".") && ($entry! = ":") && ($entry! = ". SVN")) {
  63. $new = $dir 1. " /". $entry;
  64. Echo ' Compare: '. $new. "\ n";
  65. $other = preg_replace (' #^ '. $base _dir1. ' # ', $base _dir2, $new);
  66. if (Is_dir ($new)) {
  67. Comparison
  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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.