获取指定目录下的所有文件

于 2024-04-28 发布 阅读量59  

/*
 * $str_file定义空数组
 * glob 获取文件目录,组成数组
 * is_dir 判断是否是目录
 * filesize()获取文件大小 单位B
 * fileatime():文件最后访问时间,返回时间戳
 * filectime():文件最后改变时间,返回时间戳
 * filemtime():文件最后修改时间,返回时间戳
 */
$dir =  $_SERVER['DOCUMENT_ROOT']."/template/pc/";
$str_file = $templates = array();
$files = glob($dir.'*');
foreach($files as  $val=>$a){
	if(is_dir($a)){
		$dirid = basename($a);
		$str_file[$dirid]['dirname'] = $dirid;// 文件夹名称
		$str_file[$dirid]['mod'] = substr(base_convert(fileperms($a), 10, 8), -4);//获取文件权限
		$str_file[$dirid]['filesize'] = round(filesize($a)/1024,2); //文件大小KB
		$str_file[$dirid]['fileatime'] = date("Y-m-d h:i:s",fileatime($a)); //文件最后访问时间
		$str_file[$dirid]['filectime'] = date("Y-m-d h:i:s",filectime($a)); //文件最后改变时间
		$str_file[$dirid]['filemtime'] = date("Y-m-d h:i:s",filemtime($a)); //文件最后修改时间
	}else{
		$dirid = basename($a);
		$templates[$dirid]['dirname'] = $dirid;// 文件名称
		$templates[$dirid]['mod'] = substr(base_convert(fileperms($a), 10, 8), -4);//获取文件权限
		$templates[$dirid]['filesize'] = round(filesize($a)/1024,2); //文件大小KB
		$templates[$dirid]['fileatime'] = date("Y-m-d h:i:s",fileatime($a)); //文件最后访问时间
		$templates[$dirid]['filectime'] = date("Y-m-d h:i:s",filectime($a)); //文件最后改变时间
		$templates[$dirid]['filemtime'] = date("Y-m-d h:i:s",filemtime($a)); //文件最后修改时间
	}
}
if($str_file) ksort($str_file);
if($templates) ksort($templates);
echo " <pre>";
print_r($str_file);
print_r($templates);