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
- /**
- * Tool Files
- * Purpose is to compare two folders recursively
- *
- * Call Example
- * PHP COMPARE_FOLDER.PHP/HOME/TEMP/2/HOME/TEMP/55
- *
- */
- Parameter determination
- if (count ($argv) > 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 a method that does not have the latter or the wrong one.
- Process_compare ($dir 1, $dir 2, 0);
- echo "===========================================================\n";
- Check for extra folders or files on route 2nd
- Process_compare ($dir 2, $dir 1, 1);
- echo "All ok\n";
- /**
- * Remove the/At the end of the path and make sure 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 function, a recursive method is called to implement the comparison
- *
- * @param string $dir 1 as the standard path
- * @param string $dir 2 comparison path
- * @param int $only _check_has to 1 to not compare file differences, 0 to compare file 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 functions, private functions
- *
- * @param string $dir 1 path 1, is the standard
- * @param string $base _dir1 unchanged parameter path 2
- * @param string $base _dir2 unchanged path to be compared 2
- * @param int $only _check_has to 1 to not compare file differences, 0 to compare file 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! = ":") && ($entry! = ". SVN")) {
- $new = $dir 1. " /". $entry;
- Echo ' Compare: '. $new. "\ n";
- $other = preg_replace (' #^ '. $base _dir1. ' # ', $base _dir2, $new);
- if (Is_dir ($new)) {
- Comparison
- 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 a 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);
- }
- }
- }
- ?>
Copy Code |