在遞迴掃描資料夾樹狀目錄的問題上,還是 Glob 函數的表現好一點,很準確的說。 > Scandir 函數會莫名其妙掃描兩次 ../ 處的檔案,也就是說如果小邪有倆檔案。
> ../b.php教程 和 ../a.php,結果就會在掃描報告上面出現兩次,很是奇怪。
| 代碼如下 |
複製代碼 |
//Update at 2010.07.25 - 以下代碼作廢 $path = '..'; function get_filetree_scandir($path){ $tree = array(); foreach(scandir($path) as $single){ if(is_dir('../'.$single)){ $tree = array_merge($tree,get_filetree($single)); } else{ $tree[] = '../'.$single; } } return $tree; } print_r(get_filetree_scandir($path)); //Update at 2010.07.25 - 以下為新代碼 $path = './'; function get_filetree_scandir($path){ $result = array(); $temp = array(); if (!is_dir($path)||!is_readable($path)) return null; //檢測目錄有效性 $allfiles = scandir($path); //擷取目錄下所有檔案與檔案夾 foreach ($allfiles as $filename) { //遍曆一遍目錄下的檔案與檔案夾 if (in_array($filename,array('.','..'))) continue; //無視 . 與 .. $fullname = $path.'/'.$filename; //得到完整檔案路徑 if (is_dir($fullname)) { //是目錄的話繼續遞迴 $result[$filename] = get_filetree_scandir($fullname); //遞迴開始 } else { $temp[] = $filename; //如果是檔案,就存入數組 } } foreach ($temp as $tmp) { //把臨時數組的內容存入儲存結果的數組 $result[] = $tmp; //這樣可以讓檔案夾排前面,檔案在後面 } return $result; } print_r(get_filetree_scandir($path)); |
http://www.bkjia.com/PHPjc/631703.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631703.htmlTechArticle在遞迴掃描資料夾樹狀目錄的問題上,還是 Glob 函數的表現好一點,很準確的說。 > Scandir 函數會莫名其妙掃描兩次 ../ 處的檔案,也就是說如果小...