php
//遍歷當前文件夾下全部文件的和文件夾,並以樹裝形式顯示
//1.打開文件夾句柄。獲取句柄資源
//2.讀取句柄資源,並顯示當前和子文件夾下的(文件夾和文件名)
function getDirFile($path){
if(!($file_handler=opendir($path)))
return;
$fileNTimes=array();
//遍歷-當前文件夾的"文件",排除該php文件
while(false !== ($file=readdir($file_handler))){
if($file=='.' || $file=='..' || $file=='index.php')
continue;
$fileNTimes[filemtime($path.'/'.$file)]=$file;
}
//倒序
krsort($fileNTimes);
foreach ($fileNTimes as $mtime=>$file)
{
$file_path="$path/$file";//路徑
$rel_path=str_replace(__DIR__."/", "", $file_path);//相對路徑
//若為-文件夾
if(is_dir($file_path)){
//依據"文件夾級別"縮進
if(substr_count($file_path,"/")>1){
$count=str_repeat(" ",substr_count($file_path,"/"));
echo $count.'+'.$file;
}else{
echo '+'.$file;
}
echo "
";
getDirFile($file_path);
}
//若為-文件
else{
if(substr_count($file_path,"/")>1){
$count=str_repeat(" ",substr_count($file_path,"/"));
echo $count.getFile_html($rel_path,$file).getTime_html($mtime);
}else{
echo getFile_html($file,$file).getTime_html($mtime);
}
echo "
";
}
}
}
function getTime_html($time){
return ' '.date('(Y-m-d H:m:s)',$time).'';
}
function getFile_html($rel_path,$file){
return ''.$file.'';
}
//-----------------------------------------
$path=__DIR__;
getDirFile($path);
?
>