Use php to compare the similarities and differences between the two folders

Source: Internet
Author: User
Use php to compare the similarities and differences between the two folders
Requirement: you can only use command lines to compare the differences between two folders, including the differences between files.
Thinking: Although there is diff in linux .... Use php. the code is easy to modify and the speed is fast. the comparison files in the. svn directory should compare the md5 checksum.
Idea: 1) use the first path as the standard path to list files or folders not found in 1st paths or different files. 2) then, list all the files and folders that do not exist in the 2nd paths.
Call example: php compare_folder.php/home/temp/2/home/temp/55 // reprinted from JAVAEYE

  1. /**
  2. * Tool files
  3. * The purpose is to recursively compare two folders
  4. *
  5. * Call example
  6. * Php compare_folder.php/home/temp/2/home/temp/55
  7. *
  8. */
  9. // Confirm the parameter
  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 whether the first path has a method that does not exist or is incorrect.
  19. Process_compare ($ dir1, $ dir2, 0 );
  20. Echo "============================================== =====================================\ n ";
  21. // Check the excess folders or files in the 2nd paths
  22. Process_compare ($ dir2, $ dir1, 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 ('The parameter must be an absolute path ');
  34. }
  35. $ Dir = preg_replace ('#/$ #', '', $ dir );
  36. Return $ dir;
  37. }
  38. /**
  39. * A common function calls a recursive method for comparison.
  40. *
  41. * @ Param string $ dir1 as the standard path
  42. * @ Param string $ path used for comparison of dir2
  43. * @ Param int $ only_check_has: 1 indicates no Comparison of file differences. 0 indicates comparison of the md5 checksum of the file.
  44. */
  45. Function process_compare ($ dir1, $ dir2, $ only_check_has ){
  46. Compare_file_folder ($ dir1, $ dir1, $ dir2, $ only_check_has );
  47. }
  48. /**
  49. * Real functions, private functions
  50. *
  51. * @ Param string $ dir1 path 1, standard
  52. * @ Param string $ base_dir1 constant parameter path 2
  53. * @ Param string $ base_dir2: unchanged path to be compared 2
  54. * @ Param int $ only_check_has: 1 indicates no Comparison of file differences. 0 indicates comparison of the md5 checksum of the file.
  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, 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. ?>

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.