2

this code showing directory only but i want to show directory with counts files inside the folder

example count files inside the folder

  • FolderA (2 Files Found)
  • FolderB (19 Files Found)
  • FolderD (1 Files Found)
  • FolderD (13 Files Found)

    function folderlist(){
      $startdir = './';
      $ignoredDirectory[] = '.'; 
      $ignoredDirectory[] = '..';
       if (is_dir($startdir)){
           if ($dh = opendir($startdir)){
               while (($folder = readdir($dh)) !== false){
                   if (!(array_search($folder,$ignoredDirectory) > -1)){
                     if (filetype($startdir . $folder) == "dir"){
                           $directorylist[$startdir . $folder]['name'] = $folder;
                           $directorylist[$startdir . $folder]['path'] = $startdir;
                       }
                   }
               }
               closedir($dh);
           }
       }
    return($directorylist);
    }
    
    
    $folders = folderlist();
        foreach ($folders as $folder){
        $path = $folder['path'];
        $name = $folder['name'];
    
    echo '<li><a href="' .$path .'index.php?album=' .$name . '" class="style1"><font face="Tahoma" class="ws10"><B>' . $name . '</B></a><br /></li>';
      }
    ?>
    
4

2 回答 2

3

这样的事情应该可以帮助你:

$count = iterator_count(new DirectoryIterator($path));

http://au1.php.net/iterator_count

所以:

<?php
function folderlist(){
  $directoryist = array();
  $startdir = './';
  $ignoredDirectory[] = '.';
  $ignoredDirectory[] = '..';
   if (is_dir($startdir)){
       if ($dh = opendir($startdir)){
           while (($folder = readdir($dh)) !== false){
               if (!(array_search($folder,$ignoredDirectory) > -1)){
                 if (filetype($startdir . $folder) == "dir"){
                       $directorylist[$startdir . $folder]['name'] = $folder;
                       $directorylist[$startdir . $folder]['path'] = $startdir;
                   }
               }
           }
           closedir($dh);
       }
   }
return($directorylist);
}


$folders = folderlist();

$total_files = 0;
foreach ($folders as $folder){
    $path = $folder['path'];
    $name = $folder['name'];
    $count = iterator_count(new DirectoryIterator($path . $name));
    $total_files += $count;

    echo '<li>';
    echo '<a href="' .$path .'index.php?album=' .$name . '" class="style1">';
    echo '<strong>' . $name . '</strong>';
    echo ' (' . $count . ' files found)';
    echo '</a>';
    echo '</li>';
}
  echo "Total Files:". $total_files;
?>
于 2012-11-18T06:33:54.873 回答
0

使用 Sizeof()

http://php.net/manual/en/function.sizeof.php

<?php
    function folderlist(){
      $startdir = './';
      $ignoredDirectory[] = '.'; 
      $ignoredDirectory[] = '..';
       if (is_dir($startdir)){
           if ($dh = opendir($startdir)){
               while (($folder = readdir($dh)) !== false){
                   if (!(array_search($folder,$ignoredDirectory) > -1)){
                     if (filetype($startdir . $folder) == "dir"){
                           $directorylist[$startdir . $folder]['name'] = $folder;
                           $directorylist[$startdir . $folder]['path'] = $startdir;
                       }
                   }
               }
               closedir($dh);
           }
       }
    return($directorylist);
    }


    $folders = folderlist();

        foreach ($folders as $folder){
        $path = $folder['path'];
        $name = $folder['name'];



    echo '<li><a href="' .$path .'index.php?album=' .$name . '" class="style1"><font face="Tahoma" class="ws10"><B>' . $name . '</B></a><br /></li>';
      }
      echo "Total Files:". sizeof($folders);
    ?>
于 2012-11-18T06:30:52.493 回答