>  기사  >  백엔드 개발  >  【php】读取"文件列表"按时间倒序展示,并递归显示各层目录、

【php】读取"文件列表"按时间倒序展示,并递归显示各层目录、

WBOY
WBOY원래의
2016-06-13 12:01:511053검색

【php】读取"文件列表"按时间倒序显示,并递归显示各层目录、!

思路:

1.读取该php所在目录的文件列表,用"修改时间、文件名"做键值对,塞入数组。对"修改时间"倒序。(貌似不能直接按时间倒序读取文件列表,此处为间接方法)

2.读取的若为文件直接输出,为目录就输出目录并递归扫描其下文件。


<?php //遍历当前目录下所有文件的和目录,并以树装形式显示//1.打开目录句柄,获取句柄资源//2.读取句柄资源,并显示当前和子目录下的(目录和文件名称)function getDirFile($path){	if(!($file_handler=opendir($path)))		return;		$fileNTimes=array();	//遍历-当前目录的"文件",排除该php文件	while(false !== ($file=readdir($file_handler))){		if($file==&#39;.&#39; || $file==&#39;..&#39; || $file==&#39;index.php&#39;)			continue;		$fileNTimes[filemtime($path.&#39;/&#39;.$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 "<br>";			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 "<br>";		}	}}function getTime_html($time){	return '<a style="font-size:10px;color:grey"> '.date('(Y-m-d H:m:s)',$time).'</a>';}function getFile_html($rel_path,$file){	return '<a href="'.%24rel_path.'">'.$file.'</a>';}//-----------------------------------------$path=__DIR__;getDirFile($path);?>


效果:




성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.