• <tfoot id='5qZCf'></tfoot>

    1. <small id='5qZCf'></small><noframes id='5qZCf'>

        <i id='5qZCf'><tr id='5qZCf'><dt id='5qZCf'><q id='5qZCf'><span id='5qZCf'><b id='5qZCf'><form id='5qZCf'><ins id='5qZCf'></ins><ul id='5qZCf'></ul><sub id='5qZCf'></sub></form><legend id='5qZCf'></legend><bdo id='5qZCf'><pre id='5qZCf'><center id='5qZCf'></center></pre></bdo></b><th id='5qZCf'></th></span></q></dt></tr></i><div id='5qZCf'><tfoot id='5qZCf'></tfoot><dl id='5qZCf'><fieldset id='5qZCf'></fieldset></dl></div>

        <legend id='5qZCf'><style id='5qZCf'><dir id='5qZCf'><q id='5qZCf'></q></dir></style></legend>
          <bdo id='5qZCf'></bdo><ul id='5qZCf'></ul>
      1. PHP计数目录和子目录中的总文件功能

        时间:2023-07-16

          <small id='3XmDj'></small><noframes id='3XmDj'>

          • <bdo id='3XmDj'></bdo><ul id='3XmDj'></ul>

              <tbody id='3XmDj'></tbody>
            <i id='3XmDj'><tr id='3XmDj'><dt id='3XmDj'><q id='3XmDj'><span id='3XmDj'><b id='3XmDj'><form id='3XmDj'><ins id='3XmDj'></ins><ul id='3XmDj'></ul><sub id='3XmDj'></sub></form><legend id='3XmDj'></legend><bdo id='3XmDj'><pre id='3XmDj'><center id='3XmDj'></center></pre></bdo></b><th id='3XmDj'></th></span></q></dt></tr></i><div id='3XmDj'><tfoot id='3XmDj'></tfoot><dl id='3XmDj'><fieldset id='3XmDj'></fieldset></dl></div>

          • <tfoot id='3XmDj'></tfoot>

                  <legend id='3XmDj'><style id='3XmDj'><dir id='3XmDj'><q id='3XmDj'></q></dir></style></legend>
                  本文介绍了PHP计数目录和子目录中的总文件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要获取指定目录中 JPG 文件的总数,包括它的所有子目录.没有子目录.

                  结构如下:

                  <前>目录 1/2个文件子目录 1/8个文件

                  总共 dir1 = 10 个文件

                  <前>目录2/5个文件子目录 1/2个文件子目录 2/8个文件

                  总共 dir2 = 15 个文件

                  我有这个功能,它不能正常工作,因为它只计算最后一个子目录中的文件,总数是实际文件数量的 2 倍.(如果我在最后一个子目录中有 40 个文件,将输出 80)

                  公共函数count_files($path) {全局 $file_count;$file_count = 0;$dir = opendir($path);如果 (!$dir) 返回 -1;而 ($file = readdir($dir)) :if ($file == '.' || $file == '..') continue;如果 (is_dir($path . $file)) :$file_count += $this->count_files($path . "/" . $file);别的 :$file_count++;万一;终了;关闭 ($dir);返回 $file_count;}

                  解决方案

                  为了好玩,我把它放在一起:

                  class FileFinder{私人 $onFound;私有函数 __construct($path, $onFound, $maxDepth){//onFound 在每个找到的文件时被调用$this->onFound = $onFound;//立即开始迭代$this->iterate($path, $maxDepth);}私有函数迭代($path,$maxDepth){$d = opendir($path);而 ($e = readdir($d)) {//跳过特殊文件夹if ($e == '.' || $e == '..') { 继续;}$absPath = "$path/$e";如果(is_dir($absPath)){//在进入下一次递归之前先检查 $maxDepth如果($maxDepth != 0){//减少下一次迭代的最大深度$this->iterate($absPath, $maxDepth - 1);}} 别的 {//找到常规文件,调用找到的处理程序call_user_func_array($this->onFound, array($absPath));}}关闭 ($d);}//实例化一个 finder 对象的辅助函数//虽然返回值不是很重要,因为所有方法都是私有的公共静态函数 find($path, $onFound, $maxDepth = 0){return new self($path, $onFound, $maxDepth);}}//开始查找文件(最大深度为向下一个文件夹)$count = $bytes = 0;FileFinder::find('.', function($file) use (&$count, &$bytes) {//闭包更新到目前为止的计数和字节数++$计数;$bytes += 文件大小($file);}, 1);echo "Nr 个文件:$count;使用的字节数:$bytes
                  ";

                  您传递基本路径、找到的处理程序和最大目录深度(-1 表示禁用).找到的处理程序是您在外部定义的一个函数,它从 find() 函数中给出的路径中获取相对路径名.

                  希望它有意义并能帮助你:)

                  I need to get a total count of JPG files within a specified directory, including ALL it's subdirectories. No sub-sub directories.

                  Structure looks like this :

                  dir1/
                  2 files  
                     subdir 1/
                         8 files
                  

                  total dir1 = 10 files

                  dir2/ 
                      5 files  
                      subdir 1/ 
                          2 files  
                      subdir 2/ 
                          8 files
                  

                  total dir2 = 15 files

                  I have this function, which doesn't work fine as it only counts files in the last subdirectory, and total is 2x more than the actual amount of files. (will output 80 if I have 40 files in the last subdir)

                  public function count_files($path) { 
                  global $file_count;
                  
                  $file_count = 0;
                  $dir = opendir($path);
                  
                  if (!$dir) return -1;
                  while ($file = readdir($dir)) :
                      if ($file == '.' || $file == '..') continue;
                      if (is_dir($path . $file)) :
                          $file_count += $this->count_files($path . "/" . $file);
                      else :
                          $file_count++;
                      endif;
                  endwhile;
                  
                  closedir($dir);
                  return $file_count;
                  }
                  

                  解决方案

                  For the fun of it I've whipped this together:

                  class FileFinder
                  {
                      private $onFound;
                  
                      private function __construct($path, $onFound, $maxDepth)
                      {
                          // onFound gets called at every file found
                          $this->onFound = $onFound;
                          // start iterating immediately
                          $this->iterate($path, $maxDepth);
                      }
                  
                      private function iterate($path, $maxDepth)
                      {
                          $d = opendir($path);
                          while ($e = readdir($d)) {
                              // skip the special folders
                              if ($e == '.' || $e == '..') { continue; }
                              $absPath = "$path/$e";
                              if (is_dir($absPath)) {
                                  // check $maxDepth first before entering next recursion
                                  if ($maxDepth != 0) {
                                      // reduce maximum depth for next iteration
                                      $this->iterate($absPath, $maxDepth - 1);
                                  }
                              } else {
                                  // regular file found, call the found handler
                                  call_user_func_array($this->onFound, array($absPath));
                              }
                          }
                          closedir($d);
                      }
                  
                      // helper function to instantiate one finder object
                      // return value is not very important though, because all methods are private
                      public static function find($path, $onFound, $maxDepth = 0)
                      {
                          return new self($path, $onFound, $maxDepth);
                      }
                  }
                  
                  // start finding files (maximum depth is one folder down) 
                  $count = $bytes = 0;
                  FileFinder::find('.', function($file) use (&$count, &$bytes) {
                      // the closure updates count and bytes so far
                      ++$count;
                      $bytes += filesize($file);
                  }, 1);
                  
                  echo "Nr files: $count; bytes used: $bytes
                  ";
                  

                  You pass the base path, found handler and maximum directory depth (-1 to disable). The found handler is a function you define outside, it gets passed the path name relative from the path given in the find() function.

                  Hope it makes sense and helps you :)

                  这篇关于PHP计数目录和子目录中的总文件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:统计数据库中的注册用户 下一篇:php 解析 sdk ->你好世界错误

                  相关文章

                1. <i id='jB3dj'><tr id='jB3dj'><dt id='jB3dj'><q id='jB3dj'><span id='jB3dj'><b id='jB3dj'><form id='jB3dj'><ins id='jB3dj'></ins><ul id='jB3dj'></ul><sub id='jB3dj'></sub></form><legend id='jB3dj'></legend><bdo id='jB3dj'><pre id='jB3dj'><center id='jB3dj'></center></pre></bdo></b><th id='jB3dj'></th></span></q></dt></tr></i><div id='jB3dj'><tfoot id='jB3dj'></tfoot><dl id='jB3dj'><fieldset id='jB3dj'></fieldset></dl></div>

                  <legend id='jB3dj'><style id='jB3dj'><dir id='jB3dj'><q id='jB3dj'></q></dir></style></legend>
                    • <bdo id='jB3dj'></bdo><ul id='jB3dj'></ul>
                    <tfoot id='jB3dj'></tfoot>

                    <small id='jB3dj'></small><noframes id='jB3dj'>