PHP文件树 - 列出总文件

时间:2015-01-21 04:39:37

标签: php

PHP脚本

<?php
    echo '<ul class="DirView"><li><a href="#">Recently Used<span>28</span></a></li>';
    $path = "../Desktop/IMG/BananzaNews/";
    $dir = new DirectoryIterator($path);
    foreach ($dir as $fileinfo) {
        if ($fileinfo->isDir() && !$fileinfo->isDot()) {
            $subPath = $path.$fileinfo->getFilename();
            $subDir = new DirectoryIterator($subPath);
            $count = count(glob($subPath. "/*.*"));
            echo '<li><a href="#">'.$fileinfo->getFilename().'<span>'.$count.'</span></a><ul class="sub-menu">';
            foreach ($subDir as $subPath) {
                if ($subPath->isDir() && !$subPath->isDot()) {
                    $file = $subPath->getFilename();
                    $fullPath = $path.$subPath.'/'.$file.'/*.*';
                    $inFullPath = count(glob($fullPath. '/*.*'));
                    echo '<li><a href="#">'.$file.'<span>44</span></a><li>';
                }
            }
            echo '</ul></li>';
        }
    }
    echo '</ul>';
?>

结果

My PHP File Tree Preview

问题

只忽略最近使用过的&#39;类别,其余完全读取我的目录,为了测试目的重复。如您所见,每个sub_category都有44个文件。

我现在要做的是计算总文件数(44 + 44)并将其显示在父类别(88)上,但是因为我已将此文件写入我的页面,这是可能还是有点需要重新编写脚本吗?

1 个答案:

答案 0 :(得分:1)

试试这个。替换你的所有代码。

 <?php
echo '<ul class="DirView"><li><a href="#">Recently Used<span>28</span></a></li>';
$path = "./images/";
$dir = new DirectoryIterator($path);


    foreach ($dir as $fileinfo) {
        if ($fileinfo->isDir() && !$fileinfo->isDot()) {
            $subPath = $path.$fileinfo->getFilename();
            $subDir = new DirectoryIterator($subPath);
            $count = count(glob($subPath. "/*.*"));

            $SubCount = 0;
            $subCategory = '';
            foreach ($subDir as $subDirPath) {

                if ($subDirPath->isDir() && !$subDirPath->isDot()) {
                    $subDirPathDetails = $subPath.'/'.$subDirPath->getFilename();
                $subDirDetails = new DirectoryIterator($subDirPathDetails);
                $subDircount = count(glob($subDirPathDetails. "/*.*"));

                    $SubCount = $SubCount + $subDircount;
                    $subCategory.= '<li><a href="#">'.$subDirPath->getFilename().'<span>'.$subDircount.'</span></a></li>';

                }

            }
        echo $mainCategory = '<li><a href="#">'.$fileinfo->getFilename().'<span>'.$SubCount.'</span></a><ul class="sub-menu">';
        if(!empty($subCategory)){
             echo $subCategory; // sub category li*/
        }
            echo '</ul></li>';

        }

    }
    echo '</ul>';

?>