Sian 发表于 2015-12-28 10:54:18

写一个简单函数计算目录文件总数

本帖最后由 Sian 于 2015-12-28 16:03 编辑

<?php
      // 计算文件总数
      function sareadfile($path){
                // 打开目录
                $dir = opendir($path);
                $filecount = 0;
                while($filename = readdir($dir)){
                        // 排除.与..目录,以免进入死循环
                        if ($filename == '.' || $filename == '..') continue;
                        // 拼接全路径,方便系统函数判断目录还是文件
                        $filename = $path.'/'.$filename;
                        if (is_dir($filename)){
                              // 如果是目录则递归
                              $filecount += sareadfile($filename);
                        }else{
                              // 如果是文件则自增1
                              $filecount++;
                        }
                }
                // 关闭目录
                closedir($dir);
                // 返回计算的文件总数
                return $filecount;
      }
      
      
      // 定义路径
      $path = '../html';
      // 调用函数
      echo sareadfile($path);

递归思想:http://www.yusian.com/thread-11034-1-1.html
页: [1]
查看完整版本: 写一个简单函数计算目录文件总数