【问题标题】:How to count number of files in folder in php?如何在php中计算文件夹中的文件数?
【发布时间】:2013-09-03 13:31:12
【问题描述】:

我想让用户在自己的文件夹中上传一些文件(图片)。但这只有在该文件夹包含的图片少于五张时才有可能。如果已经有 5 张图片,脚本必须让用户知道他/她的文件夹已满。 所以,我想知道php中是否有计算文件夹中文件数量的函数。或者 php 中的任何其他方式来做到这一点?提前致谢。

【问题讨论】:

标签: php directory


【解决方案1】:

如图所示使用FilesystemIterator

$dir = "/path/to/folder";
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$fileCount = iterator_count($fi);

【讨论】:

  • 不错的替代方法方法
  • @DonCallisto 我想我在 stackoverflow 上看到了另一个类似问题的答案,从那以后我就一直在使用它,而且效果很好
【解决方案2】:

没有比这更简单的了:使用 opendir()readdir() 就像关注一样:

<?php

$images_extension_array = array("jpg","jpeg","gif","png");

$dir = "/path/to/user/folder";
$dir_resource = opendir($dir);

$file_count = 0;
while (($file = readdir($dir_resource)) !== false) { // scan directory
  $extension_from = strrpos($file,"."); // isolate extension index/offset
  if ($extension_from && in_array(substr($file,$extension_from+1), $images_extension_array))
    $file_count ++; //if has extension and that extension is "associated" with an image, count
}
if ($number_of_files == %) {
  //do stuff
}

显然这并没有考虑文件扩展名...


你也可以使用:

  • scandir() ---> read here
  • FilesystemIterator 类(正如 dops 的正确回答所暗示的那样)---> read here

【讨论】:

    【解决方案3】:

    您可以让 PHP 为您找到文件……然后计算它们。

    $count = count(glob("$path_to_user_dir/*"));
    

    【讨论】:

    • 这是“insertusernamehere”评论的副本
    • @Pierre:非常相似,是的。在我看到之前,我实际上正要包含一些关于如何仅查找图像文件的信息。好吧,在我意识到在 *nix 上,通过扩展进行 globbing 会变得不确定。 (例如,区分大小写的操作系统和/或文件系统不会选择 whatever.JPG;您必须包含大小写的每种组合,或者至少包含常见的组合。)
    【解决方案4】:

    我真的很喜欢 dops 的回答,但它会返回文件、目录和符号链接的计数,这可能不是目标。如果您只想计算目录中本地文件的数量,可以使用:

    $path = "/path/to/folder";
    $fs = new FilesystemIterator($path);
    foreach($fs as $file) {
       $file->isFile() ?  ++$filecount : $filecount;
    }
    

    【讨论】:

      【解决方案5】:

      你可以使用

      $nbFiles=count(scandir('myDirectory'))-2;
      

      (-2 用于删除“.”和“..”)

      【讨论】:

        【解决方案6】:

        这里的这个小功能是对我不久前发现的一些代码的修改,它还将计算所有子文件夹以及这些文件夹中的所有内容:

        <?PHP
        
        
        $folderCount = $fileCount = 0;
        
        countStuff('.', $fileCount, $folderCount);
        
        function countStuff($handle, &$fileCount, &$folderCount)
        {
            if ($handle = opendir($handle)) {
                while (false !== ($entry = readdir($handle))) {
                    if ($entry != "." && $entry != "..") {
                        if (is_dir($entry)) {
                            echo "Folder => " . $entry . "<br>";
                            countStuff($entry, $fileCount, $folderCount);
                            $folderCount++;
                        } else {
                            echo "File   => " . $entry . "<br>";
                            $fileCount++;
                        }
                    }
                }
                closedir($handle);
            }
        }
        echo "<br>==============<br>";
        echo "Total Folder Count : " . $folderCount . "<br>";
        echo "Total File Count : " . $fileCount;
        
        ?>
        

        注意:我还将发布原始代码,该代码将仅计算父目录的文件和文件夹,而不是下面的子文件夹子文件夹:

        if ($handle = opendir('.')) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry != "." && $entry != "..") {
                    if (is_dir($entry)) {
                        echo "Folder => " . $entry . "<br>";
                        countStuff($entry, $fileCount, $folderCount);
                        $folderCount++;
                    } else {
                        echo "File   => " . $entry . "<br>";
                        $fileCount++;
                    }
                }
            }
            echo "<br>==============<br>";
            echo "Total Folder Count : " . $folderCount . "<br>";
            echo "Total File Count : " . $fileCount;
            closedir($handle);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-06
          • 2013-09-16
          • 2016-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-06
          • 1970-01-01
          相关资源
          最近更新 更多