0

PHP Script

<?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>';
?>

Result

My PHP File Tree Preview

The Problem

Only ignoring the 'recently used' category, the rest fully reads my directories of which are duplicated for testing purposes. As you can see, each sub_category has 44 files inside.

What I now want to do is count the total files (44 + 44) and display them on their parent category (88) however as I've already written this to my page, is this possible or is a bit of re-scripting needed?

Tim Marshall
  • 360
  • 5
  • 25

1 Answers1

1

Try this. replace all your code.

 <?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>';

?>
kamlesh.bar
  • 1,774
  • 19
  • 38
  • So I comment that line out and add your code where? – Tim Marshall Jan 21 '15 at 05:01
  • replace your code (inner for loop only and echo statement above inner for loop) with above snippet. – kamlesh.bar Jan 21 '15 at 05:01
  • Sorry, pretty new at PHP, this is quite advanced for how little I've been learning :) – Tim Marshall Jan 21 '15 at 05:02
  • 1
    Thank you a billion, I was just trying to get my head around doing this, did part then got confused again and even more when the code changed in front of my eyes :P One of those "OMG is that the time?!!!" programming nights :P – Tim Marshall Jan 21 '15 at 05:08
  • Oh no! I've just realised it's not actually counting the files correctly :( – Tim Marshall Jan 21 '15 at 05:11
  • 1
    Category_1 > Sub_Category_1 has 22 files inside and Category_1 > Sub_Category_2 has 14 files inside... – Tim Marshall Jan 21 '15 at 05:13
  • Sorry, I never meant the adding of both, your adding of both prints perfectly but I thought my code was working perfectly adding how many files there were in the sub-categories... – Tim Marshall Jan 21 '15 at 05:13
  • for counting file inside folder do check (http://stackoverflow.com/questions/12801370/count-how-many-files-in-directory-php). – kamlesh.bar Jan 21 '15 at 05:18
  • Okay and you should revert your code back as this breaks the script and no-longer adds up – Tim Marshall Jan 21 '15 at 05:19
  • your current code does not work - It breaks my page. – Tim Marshall Jan 21 '15 at 05:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69273/discussion-between-tim-marshall-and-kamlesh-bar). – Tim Marshall Jan 21 '15 at 05:45