国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php實現(xiàn)比較兩個文件夾異同的方法

2024-05-04 23:36:32
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php實現(xiàn)比較兩個文件夾異同的方法,涉及php針對目錄與文件名的遞歸操作技巧,需要的朋友可以參考下

本文實例講述了php實現(xiàn)比較兩個文件夾異同的方法。分享給大家供大家參考。具體分析如下:

要求:

只能使用命令行,比較兩個文件夾的不同,包括文件的差異。

思考:

雖然linux下有diff。。。。還是用php吧,代碼改的方便,速度也很快,以下排除了.svn目錄的比較

文件要比較md5校驗和

思路:

1)把第一路徑作為標準路徑,列出第1個路徑中有的,第2個路徑中沒有的文件或文件夾,或者是不同的文件。

2)然后,列出第2個路徑中有的,第1個路徑中卻不存在的文件和文件夾。

調(diào)用示例:

php compare_folder.php /home/temp/2 /home/temp/55

代碼如下:

 

 
  1. <?php  
  2. /**  
  3. * 工具文件  
  4. * 目的在于遞歸比較兩個文件夾  
  5.  
  6. * 調(diào)用示例  
  7. * php compare_folder.php /home/temp/2 /home/temp/55  
  8.  
  9. */ 
  10. //參數(shù)確定  
  11. if (count($argv) > 1 )  
  12. $dir1 = del_postfix($argv[1]);  
  13. else 
  14. $dir1 = '/';  
  15. if (count($argv) > 2 )  
  16. $dir2 = del_postfix($argv[2]);  
  17. else 
  18. $dir2 = '/';  
  19. //檢查第一個路徑有,后者沒有或錯誤的方法。  
  20. process_compare($dir1$dir2, 0);  
  21. echo "===========================================================/n";  
  22. //檢查第2個路徑的多余文件夾或文件  
  23. process_compare($dir2 , $dir1, 1);  
  24. echo "all OK/n";  
  25. /**  
  26. * 去除路徑末尾的/,并確保是絕對路徑  
  27.  
  28. * @param unknown_type $dir  
  29. * @return unknown  
  30. */ 
  31. function del_postfix($dir)  
  32. {  
  33. if (!preg_match('#^/#'$dir)) {  
  34. throw new Exception('參數(shù)必須是絕對路徑');  
  35. }  
  36. $dir = preg_replace('#/$#'''$dir);  
  37. return $dir;  
  38. }  
  39. /**  
  40. * 公用函數(shù),會調(diào)用一個遞歸方法實現(xiàn)比較  
  41.  
  42. * @param string $dir1 作為標準的路徑  
  43. * @param string $dir2 對比用的路徑  
  44. * @param int $only_check_has 為1表示不比較文件差異,為0表示還要比較文件的md5校驗和  
  45. */ 
  46. function process_compare($dir1$dir2$only_check_has){  
  47. compare_file_folder($dir1$dir1$dir2$only_check_has);  
  48. }  
  49. /**  
  50. * 真實的函數(shù),私有函數(shù)  
  51.  
  52. * @param string $dir1 路徑1,是標準  
  53. * @param string $base_dir1 不變的參數(shù)路徑2  
  54. * @param string $base_dir2 不變的待比較的路徑2  
  55. * @param int $only_check_has 為1表示不比較文件差異,為0表示還要比較文件的md5校驗和  
  56.  
  57. */ 
  58. function compare_file_folder($dir1$base_dir1$base_dir2$only_check_has=0){  
  59. if (is_dir($dir1)) {  
  60. $handle = dir($dir1);  
  61. if ($dh = opendir($dir1)) {  
  62. while ($entry = $handle->read()) {  
  63. if (($entry != ".") && ($entry != "..") && ($entry != ".svn")){  
  64. $new = $dir1."/".$entry;  
  65. //echo 'compare: ' . $new . "/n";  
  66. $other = preg_replace('#^'$base_dir1 .'#' , $base_dir2$new);  
  67. if(is_dir($new)) {  
  68. //比較  
  69. if (!is_dir($other)) {  
  70. echo '!!not found direction: '$other' (' . $new .")/n";  
  71. }  
  72. compare_file_folder($new$base_dir1,$base_dir2$only_check_has) ;  
  73. else { //如果1是文件,則2也應(yīng)該是文件  
  74. if (!is_file($other)) {  
  75. echo '!!not found file: '$other' ('.$new .")/n";  
  76. }elseif ($only_check_has ==0 && ( md5_file($other) != md5_file($new) ) ){  
  77. echo '!!file md5 error: '$other' ('.$new .")/n";  
  78. }  
  79. }  
  80. }  
  81. }  
  82. closedir($dh);  
  83. }  
  84. }  
  85. }  
  86. ?> 

希望本文所述對大家的php程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 韶山市| 凉山| 泉州市| 永善县| 乌拉特中旗| 博兴县| 噶尔县| 惠来县| 集安市| 宁南县| 正镶白旗| 项城市| 仪征市| 美姑县| 大姚县| 东城区| 濉溪县| 丰县| 万载县| 宁国市| 广平县| 吉首市| 盐池县| 称多县| 固安县| 赣州市| 昭通市| 南部县| 崇礼县| 赞皇县| 长乐市| 腾冲县| 顺昌县| 东乌珠穆沁旗| 永平县| 宁乡县| 万全县| 古田县| 金寨县| 丰城市| 邵武市|