This article mainly introduces how php compares the similarities and differences between two folders, and involves php's recursive operations on directories and file names, for more information about how to use php to compare two folders, see the example in this article. 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.