php 遞迴遍曆檔案樹代碼_PHP教程

來源:互聯網
上載者:User
在遞迴掃描資料夾樹狀目錄的問題上,還是 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 函數會莫名其妙掃描兩次 ../ 處的檔案,也就是說如果小...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.