以表格的形式显示
<?php
/**
* Created by PhpStorm.
* User: lc
* Date: 2020/10/21
* Time: 15:27
*/
$dirname = 'E:\作业2020\php\4626'; //指定要遍历的目录
//将遍历的目录和文件名使用表格格式输出
echo '<table border="1px solid" align="center" width="1000" cellspacing="0" cellpadding="0">';
echo '<caption><h2>目录'.$dirname.'下内容</h2></caption>';
echo '<tr align="center" bgcolor="#cccccc">';
echo '<th>文件名</th><th>文件大小/bytes</th><th>文件大小/KB</th><th>文件类型</th><th>修改时间</th>';
//使用scandir()读取目录里的内容
function traverse($dirname){
$dir_temp = scandir($dirname);
//遍历文件夹
foreach($dir_temp as $v){
$a = $dirname.'/'.$v;
if(is_dir($a)){//判断是否为系统的隐藏文件./.. 如果是则跳过否则就继续往下走
if($v == '.' || $v == '..'){
continue;
}
echo '<tr bgcolor="red" align="center">';
echo '<td>' . basename($a) . '</td>'; //显示文件名
echo '<td>' . filesize($a) .'b'. '</td>'; //显示文件大小,单位byte
echo '<td>' .round(filesize($a)/1024,2).'KB'. '</td>';//显示未见大小,Mb
echo '<td>' . '文件夹' . '</td>'; //显示文件类型
echo '<td>' . date("Y/n/t", filemtime($a)) . '</td>'; //格式化显示文件修改时间
echo '</tr>';
traverse($a);
}else{
echo '<tr align="center">';
echo '<td>' . basename($a) . '</td>'; //显示文件名
echo '<td>' . filesize($a) .'b'. '</td>'; //显示文件大小,单位byte
echo '<td>' .round(filesize($a)/1024,2).'KB'. '</td>';//显示未见大小,Mb
echo '<td>' . '文件'. '</td>'; //显示文件类型
echo '<td>' . date("Y年n月t日", filemtime($a)) . '</td>'; //格式化显示文件修改时间
echo '</tr>';
}
}
}
traverse($dirname);
echo '</table>';
运行截图: