如何计算php文件夹中的文件

时间:2012-11-18 06:25:37

标签: php directory subdirectory dir

此代码仅显示目录,但我想在文件夹

中显示带有计数文件的目录

示例 计算文件夹中的文件

  • FolderA(找到2个文件)
  • FolderB(找到19个文件)
  • FolderD(找到1个文件)
  • FolderD(找到13个文件)

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

2 个答案:

答案 0 :(得分: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;
?>

答案 1 :(得分: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);
    ?>