function echoLevel($num){ if ($num>0){ echo " "; --$num; echoLevel($num); } } function echoName($rootPath,&$arr,$index,$lastLevel){ //循環結束,退出 if (!isset($arr[$index])){ return false; } echoLevel($lastLevel); //輸出文件名稱 echo "|-".$arr[$index]."\r\n"; $nowPath=$rootPath.'\\'.$arr[$index]; //下一個文件 ++$index; //如果不是目錄,接着讀取下一個文件內容 if (is_file($nowPath)){ return echoName($rootPath,$arr,$index,$lastLevel); } if (!is_dir($nowPath)){ echo '這是不可能的!!!'; exit(); } //層級+1 $nowLevel=$lastLevel+1; //如果是目錄,進入目錄繼續查詢文件 foreachDir($nowPath,$nowLevel); //接着讀取下一個文件內容 return echoName($rootPath,$arr,$index,$lastLevel); } function foreachDir($rootPath,$lastLevel){ //目錄判斷 if(!is_dir($rootPath)) { echo "目錄《".$rootPath."》不存在"; } $dir=scandir($rootPath); //空目錄判斷 if (count($dir)<3){ // echo strrchr($rootPath,'\\')."\\\r\n"; return; } //輸出讀取目錄內容 echoName($rootPath,$dir,2,$lastLevel); } foreachDir('C:\work\project\biyouxin',0);
和朋友打賭純用遞歸寫的文件目錄讀取
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.