This article describes the PHP implementation of the comparison of two folders and similarities and differences of methods. Share to everyone for your reference. The specific analysis is as follows:
Requirements:
You can use only the command line to compare the differences between two folders, including the file.
Thinking:
Although the Linux has a diff .... or use PHP bar, code to change the convenience, speed is also very fast, the following exclusion. Comparison of SVN catalogs
File to compare MD5 checksum
Ideas:
1 The first path as a standard path, listed in the 1th path, there are no files or folders in the 2nd path, or different files.
2) Then, list the files and folders in the 2nd path that do not exist in the 1th path.
Call Example:
PHP COMPARE_FOLDER.PHP/HOME/TEMP/2/HOME/TEMP/55
The code is as follows:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 |
<?php/** * Tool File * The purpose is to recursively compare two folders * * Invoke sample * PHP COMPARE_FOLDER.PHP/HOME/TEMP/2/HOME/TEMP/55 *//parameter to determine if (count ($a RGV) > 1 $dir 1 = del_postfix ($argv [1]); else $dir 1 = '/'; if (count ($ARGV) > 2) $dir 2 = Del_postfix ($argv [2]); else $dir 2 = '/'; Check that the first path has, the latter has no or the wrong method. Process_compare ($dir 1, $dir 2, 0); echo "===========================================================n"; Check the 2nd path for extra folders or files Process_compare ($dir 2, $dir 1, 1); echo "All Okn"; /** * Removes the end of the path and ensures that it is an absolute path * * @param unknown_type $dir * @return Unknown/function Del_postfix ($dir) {if!preg_match (' #^ /# ', $dir)) {throw new Exception (' parameter must be absolute path ');} $dir = Preg_replace (' #/$# ', ', $dir); return $dir; /** * Common functions, a recursive method is invoked to implement comparison * * @param string $dir 1 as standard path * @param string $dir 2 the path * @param int $only _check_has to 1 means no Compare file differences to 0 to compare the file's MD5 checksum/function Process_compare ($dir 1, $dir 2, $only _check_has) {Compare_file_folder ($dir 1, $dir 1 , $dir 2, $only _check_has); /** * Real function, Private function * * @param string $dir 1 path 1, is standard * @param string $base _dir1 parameter Path 2 * @param string $base _DIR2 constant to be compared path 2 * @param int $only _check_has 1 indicates no comparison of file differences, 0 indicates that you want to compare files MD5 Checksum */function Compare_file_folder ($dir 1, $base _dir1, $base _dir2, $only _check_has=0) {if (Is_dir ($dir 1)) {$handle = Dir ($dir 1); if ($dh = Opendir ($dir 1)) {while ($entry = $handle->read ()) {if ($entry!= ".") && ($entry!= "...") &&am P ($entry!= ". SVN")) {$new = $dir 1. " /". $entry; Echo ' Compare: '. $new. "N"; $other = preg_replace (' #^ '. $base _dir1. # ', $base _dir2, $new); if (Is_dir ($new)) {//Compare if (!is_dir ($other)) {echo '!! Not found direction: '. $other. ' ('. $new. ") n "; Compare_file_folder ($new, $base _dir1, $base _dir2, $only _check_has); else {//if 1 is a file, then 2 should also be file if (!is_file ($other)) {Echo!! Not found file: '. $other. ' ('. $new. ") n "; }elseif ($only _check_has ==0 && (md5_file ($other)!= md5_file ($new))) {echo '!! File MD5 error: '. $other. ' ('. $new. ") n "; }}} closedir ($DH); }}}?> |
I hope this article will help you with your PHP programming.