欢迎光临
感谢一路有你

案例-遍历目录,输出所有文件文件夹(递归)

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

遍历目录,输出所有文件文件夹(递归)

<?php
header("content-type:text/html;charset=utf-8");
include_once('./getfilesize.php');
date_default_timezone_set('PRC');

function dirtab ($dir)
{
    if (!file_exists($dir)) {
        echo "目录不存在";
        return  false;
    }
    if (!is_dir($dir)) {
        echo "不是目录";
        return  false;
    }
    $res = opendir($dir);
   static $str ='';//如果不使用static,那么只输出一层,下一层的目录内容不输出
    $num =0;
    while (false !== ($filename = readdir($res))) {
        $num++;
        $file_path = rtrim($dir,'/').'/'.$filename;

        if($filename == '.' || $filename == '..'){
            continue;
        }
        if(is_dir($file_path)){
            dirtab($file_path);
        }

        $color = $num % 2 == 0 ? '#abcdef': '#fff';
        $str .= "<tr bgcolor=".$color.">";
        $str .= "<td>".$filename."</td>";
        $str .= "<td>".getfilesize(filesize($file_path))."</td>";
        $str .= "<td>".(filetype($file_path) == 'dir' ? '目录':'文件') ."</td>";
        $str .= "<td>".(date('Y-m-d H:i:s' , filectime($file_path))) ."</td>";
        $str .= "<td>".(date('Y-m-d H:i:s' , filemtime($file_path))) ."</td>";
        $str .= "<td>".(is_readable($file_path) == 1 ?'YES' : 'NO') ."</td>";
        $str .= "</tr>";
    }
    closedir($res);
    return $str;
}

echo "<table border='1' align='center' cellspacing='0'>";
echo "<tr>";
echo "<th>文件名</th>";
echo "<th>文件大小</th>";
echo "<th>类型</th>";
echo "<th>创建时间</th>";
echo "<th>修改时间</th>";
echo "<th>是否可读</th>";
echo "</tr>";
echo dirtab ('../1127');
echo "</table>";

 

 

遍历目录,输出所有文件(递归)

//加一个输出判断is_file()
 if(is_file($file_path)){
        $color = $num % 2 == 0 ? '#abcdef': '#fff';
        $str .= "<tr bgcolor=".$color.">";
        $str .= "<td>".$filename."</td>";
        $str .= "<td>".getfilesize(filesize($file_path))."</td>";
        $str .= "<td>".(filetype($file_path) == 'dir' ? '目录':'文件') ."</td>";
        $str .= "<td>".(date('Y-m-d H:i:s' , filectime($file_path))) ."</td>";
        $str .= "<td>".(date('Y-m-d H:i:s' , filemtime($file_path))) ."</td>";
        $str .= "<td>".(is_readable($file_path) == 1 ?'YES' : 'NO') ."</td>";
        $str .= "</tr>";
        }

 

赞(3) 打赏
未经允许不得转载:王明昌博客 » 案例-遍历目录,输出所有文件文件夹(递归)
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏