<? Php /** * Tool files * The purpose is to recursively compare two folders * * Call example * Php compare_folder.php/home/temp/2/home/temp/55 * */ // Confirm the Parameter 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 folders or files in the 2nd paths Process_compare ($ dir2, $ dir1, 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 ('the parameter must be an absolute path '); } $ Dir = preg_replace ('#/$ #', '', $ dir ); Return $ dir; } /** * A common function calls a recursive method for comparison. * * @ Param string $ dir1 as the standard path * @ Param string $ path used for comparison of dir2 * @ Param int $ only_check_has: 1 indicates no comparison of file differences. 0 indicates comparison of the md5 checksum of the file. */ 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, standard * @ Param string $ base_dir1 constant parameter Path 2 * @ Param string $ base_dir2: Unchanged path to be compared 2 * @ Param int $ only_check_has: 1 indicates no comparison of file differences. 0 indicates comparison of the md5 checksum of the file. * */ 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, 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 ); } } } ?> |