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

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

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

2019-11-02 15:05:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

  要求:

  只能使用命令行,比較兩個(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

  代碼如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
西瓜電影網(wǎng)[www.aikan.tv/special/xiguadianyingwang/]
81 82 83 84 85 86 <?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 OKn"; /** * 去除路徑末尾的/,并確保是絕對(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); } } } ?>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴山县| 定安县| 白沙| 个旧市| 永胜县| 淮滨县| 峡江县| 辽宁省| 双流县| 明星| 朝阳市| 德格县| 温宿县| 夹江县| 德钦县| 香格里拉县| 嘉兴市| 桐乡市| 正宁县| 宜宾市| 平江县| 鄂伦春自治旗| 舟山市| 噶尔县| 克东县| 贵港市| 土默特右旗| 外汇| 武夷山市| 朝阳区| 西乌珠穆沁旗| 罗定市| 增城市| 孝昌县| 炎陵县| 亳州市| 阿拉尔市| 民和| 淄博市| 东源县| 怀仁县|