PHP Implementation folder Differences comparison

Source: Internet
Author: User
How does PHP Implement folder difference comparison? This article mainly introduces the PHP implementation comparison of two folders and similarities and differences, involving PHP for directory and file name of the recursive operation skills. We hope to help you.

In this paper, we explain how PHP implements comparison of two folders. Share to everyone for your reference. The specific analysis is as follows:

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/55

The code is as follows:


<?php/** * Tool file * Intended to recursively compare two folders * * Call example * PHP COMPARE_FOLDER.PHP/HOME/TEMP/2/HOME/TEMP/55 * *///Parameters determine if (CO UNT ($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 the 2nd path for extra folders or files Process_compare ($dir 2, $dir 1, 1); echo "All ok\n"; /** * Remove the/At the end of the path and make sure it is the absolute path * * @param unknown_type $dir * @return Unknown */function Del_postfix ($dir) {if (!preg_mat  CH (' #^/# ', $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 standard path * @param string $dir 2 comparison path * @param int $only _check_has A 1 indicates that the file difference is not compared, and 0 indicates that the MD5 checksum of the file is also compared */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 the standard * @param string $base _dir1 unchanged parameter Path 2 * @param string $base _dir2 the path to be compared 2 * @param int $ A only_check_has of 1 indicates that the file difference is not compared, and 0 means that the MD5 checksum of the file is also compared * */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! = "...") & amp;& ($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 the 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); }}}?>

Related recommendations:

PHP File Reading Series method

PHP file Traversal

PHP folder operations

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.