溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用php怎么對(duì)兩個(gè)文件夾的異同進(jìn)行比較

發(fā)布時(shí)間:2021-01-22 15:14:16 來(lái)源:億速云 閱讀:185 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)利用php怎么對(duì)兩個(gè)文件夾的異同進(jìn)行比較,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

要求:

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

思考:

雖然linux下有diff。。。。還是用php吧,代碼改的方便,速度也很快,以下排除了.svn目錄的比較
文件要比較md5校驗(yàn)和

思路:

1)把第一路徑作為標(biāo)準(zhǔn)路徑,列出第1個(gè)路徑中有的,第2個(gè)路徑中沒(méi)有的文件或文件夾,或者是不同的文件。
2)然后,列出第2個(gè)路徑中有的,第1個(gè)路徑中卻不存在的文件和文件夾。

調(diào)用示例:

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

代碼如下:

<?php 
/** 
 * 工具文件 
 * 目的在于遞歸比較兩個(gè)文件夾 
 * 
 * 調(diào)用示例 
 * php compare_folder.php /home/temp/2 /home/temp/55 
 * 
 */ 
//參數(shù)確定 
if (count($argv) > 1 ) 
 $dir1 = del_postfix($argv[1]); 
else 
 $dir1 = '/'; 
if (count($argv) > 2 ) 
 $dir2 = del_postfix($argv[2]); 
else 
 $dir2 = '/'; 
//檢查第一個(gè)路徑有,后者沒(méi)有或錯(cuò)誤的方法。 
process_compare($dir1, $dir2, 0); 
echo "===========================================================\n"; 
//檢查第2個(gè)路徑的多余文件夾或文件 
process_compare($dir2 , $dir1, 1); 
echo "all OK\n"; 
/** 
 * 去除路徑末尾的/,并確保是絕對(duì)路徑 
 * 
 * @param unknown_type $dir 
 * @return unknown 
 */ 
function del_postfix($dir) 
{ 
 if (!preg_match('#^/#', $dir)) { 
  throw new Exception('參數(shù)必須是絕對(duì)路徑'); 
 } 
 $dir = preg_replace('#/$#', '', $dir); 
 return $dir; 
} 
/** 
 * 公用函數(shù),會(huì)調(diào)用一個(gè)遞歸方法實(shí)現(xiàn)比較 
 * 
 * @param string $dir1 作為標(biāo)準(zhǔn)的路徑 
 * @param string $dir2 對(duì)比用的路徑 
 * @param int $only_check_has 為1表示不比較文件差異,為0表示還要比較文件的md5校驗(yàn)和 
 */ 
function process_compare($dir1, $dir2, $only_check_has){ 
 compare_file_folder($dir1, $dir1, $dir2, $only_check_has); 
} 
/** 
 * 真實(shí)的函數(shù),私有函數(shù) 
 * 
 * @param string $dir1  路徑1,是標(biāo)準(zhǔn) 
 * @param string $base_dir1 不變的參數(shù)路徑2 
 * @param string $base_dir2 不變的待比較的路徑2 
 * @param int $only_check_has 為1表示不比較文件差異,為0表示還要比較文件的md5校驗(yàn)和 
 * 
 */ 
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也應(yīng)該是文件 
      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怎么對(duì)兩個(gè)文件夾的異同進(jìn)行比較了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI