編寫一個函數,傳入一個目錄字符串,使得可以遍歷該目錄下的所有文件及文件夾。
由于代碼比較簡單,所以就直接貼代碼了。
<?php
function fileShow($dir){ //遍歷目錄下的所有文件和文件夾
$handle = opendir($dir);
while($file = readdir($handle)){
if($file !== '..' && $file !== '.'){
$f = $dir.'/'.$file;
if(is_file($f)){
echo '|--'.$file.'<br>'; //代表文件
}else{
echo '--'.$file.'<br>'; //代表文件夾
fileShow($f);
}
}
}
}
fileShow('./'); //測試當前的目錄
很簡單沒騙你吧?
我的運行結果如下:
遍歷樹
啦啦啦,收工!