Php implementation to compare the similarities and differences between the two folders, _ PHP Tutorial

Source: Internet
Author: User
Php implementation compares the similarities and differences between the two folders ,. Php compares the similarities and differences between two folders. This article describes how php compares the differences between two folders. Share it with you for your reference. The specific analysis is as follows: Requirements: php compares the similarities and differences between the two folders,

This example describes how to compare the similarities and differences between two folders in php. Share it with you for your reference. The specific analysis is as follows:

Requirements:

You can only use command lines to compare the differences between two folders, including the differences in files.

Thoughts:

In linux, diff .... Use php. the code is easy to modify and the speed is fast. the comparison of. svn directories is excluded below.
File to compare the md5 checksum

Ideas:

1) use the first path as the standard path to list files or folders not found in the first path, 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

The code is as follows:

<? Php/*** tool file ** is used to recursively compare two folders ** call example ** php compare_folder.php/home/temp/2/home/temp/55 ** // The parameter is determined if (count ($ argv)> 1) $ dir1 = del_postfix ($ argv [1]); else $ dir1 = '/'; if (count ($ argv)> 2) $ dir2 = del_postfix ($ argv [2]); else $ dir2 = '/'; // check whether the first path has a method that does not exist or is incorrect. Process_compare ($ dir1, $ dir2, 0 ); echo "============================================== =====================================\ n "; // check the excess folder or file process_compare ($ dir2, $ dir1, 1) in the 2nd paths; echo "all OK \ n";/*** remove/at the end of the path /, make sure the path is absolute ** @ param unknown_type $ dir * @ return unknown */function del_postfix ($ dir) {if (! Preg_match ('# ^/#', $ dir) {throw new Exception ('The parameter must be an absolute path');} $ dir = preg_replace ('#/$ #', '', $ dir); return $ dir;}/*** public function, A recursive method will be called to achieve comparison ** @ param string $ dir1 as the standard path * @ param string $ path used for comparison between dir2 * @ param int $ only_check_has is 1, indicating that files are not compared difference, 0 indicates the md5 checksum of the file to be compared. */function process_compare ($ dir1, $ dir2, $ only_check_has) {compare_file_folder ($ dir1, $ dir1, $ dir2, $ only_check_has );} /*** real functions, private functions ** @ Param string $ dir1 path 1, it is the standard * @ param string $ base_dir1 unchanged parameter path 2 * @ param string $ base_dir2 unchanged to be compared path 2 * @ param int $ only_check_has is 1, meaning no Comparison of file differences, 0 indicates the md5 checksum of the file to be compared. **/function compare_file_folder ($ dir1, $ base_dir1, $ base_dir2, $ only_check_has = 0) {if (is_dir ($ dir1 )) {$ handle = dir ($ dir1); if ($ dh = opendir ($ dir1) {while ($ entry = $ handle-> read ()) {if ($ entry! = ".") & ($ Entry! = "...") & ($ Entry! = ". Svn ") {$ new = $ dir1 ."/". $ 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) ;}}?>

I hope this article will help you with php programming.

Examples in this article describes how to compare the similarities and differences between two folders in php. Share it with you for your reference. The specific analysis is as follows: Requirements :...

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.