php實現比較兩個檔案夾異同的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php實現比較兩個檔案夾異同的方法。分享給大家供大家參考。具體分析如下:

要求:

只能使用命令列,比較兩個檔案夾的不同,包括檔案的差異。

思考:

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

思路:

1)把第一路徑作為標準路徑,列出第1個路徑中有的,第2個路徑中沒有的檔案或檔案夾,或者是不同的檔案。
2)然後,列出第2個路徑中有的,第1個路徑中卻不存在的檔案和檔案夾。

調用樣本:

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

代碼如下:

<?php /**  * 工具檔案  * 目的在於遞迴比較兩個檔案夾  *  * 調用樣本  * php compare_folder.php /home/temp/2 /home/temp/55  *  */ //參數確定 if (count($argv) > 1 )  $dir1 = del_postfix($argv[1]); else  $dir1 = '/'; if (count($argv) > 2 )  $dir2 = del_postfix($argv[2]); else  $dir2 = '/'; //檢查第一個路徑有,後者沒有或錯誤的方法。 process_compare($dir1, $dir2, 0); echo "===========================================================\n"; //檢查第2個路徑的多餘檔案夾或檔案 process_compare($dir2 , $dir1, 1); echo "all OK\n"; /**  * 去除路徑末尾的/,並確保是絕對路徑  *  * @param unknown_type $dir  * @return unknown  */ function del_postfix($dir) {  if (!preg_match('#^/#', $dir)) {   throw new Exception('參數必須是絕對路徑');  }  $dir = preg_replace('#/$#', '', $dir);  return $dir; } /**  * 公用函數,會調用一個遞迴方法實現比較  *  * @param string $dir1 作為標準的路徑  * @param string $dir2 對比用的路徑  * @param int $only_check_has 為1表示不比較檔案差異,為0表示還要比較檔案的md5校正和  */ function process_compare($dir1, $dir2, $only_check_has){  compare_file_folder($dir1, $dir1, $dir2, $only_check_has); } /**  * 真實的函數,私人函數  *  * @param string $dir1  路徑1,是標準  * @param string $base_dir1 不變的參數路徑2  * @param string $base_dir2 不變的待比較的路徑2  * @param int $only_check_has 為1表示不比較檔案差異,為0表示還要比較檔案的md5校正和  *  */ 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)) {       //比較       if (!is_dir($other)) {        echo '!!not found direction: '. $other. ' (' . $new .")\n";       }       compare_file_folder($new, $base_dir1,$base_dir2, $only_check_has) ;      } else { //如果1是檔案,則2也應該是檔案       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);   }  } } ?>

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.