doumei4964 2015-06-09 12:09
浏览 8

计算目录中的文件,同时使用PHP通过目录和子目录排除其他文件

I have implemented a file-counting code using a function found here at SO: https://stackoverflow.com/a/640944/2554788.

The function goes thus:

function getFileCount($path) {
    $size = 0;
    $ignore = array('.','..','cgi-bin','.DS_Store');
    $files = scandir($path);
    foreach($files as $t) {
        if(in_array($t, $ignore)) continue;
        if (is_dir(rtrim($path, '/') . '/' . $t)) {
            $size += getFileCount(rtrim($path, '/') . '/' . $t);
        } else {
            $size++;
        }   
    }
    return $size;
}

It works well, until I want to exclude certain directories and all their contents. What I need is to pass:

$ignore = array('.','..','cgi-bin','.DS_Store', 'SPECIAL_DIR');

At the moment, this function omits "SPECIAL_DIR", but seems to count all its content files, since they do not textually match "SPECIAL_DIR"!

I've bumped into a couple of solutions here and elsewhere that are meant to exclude files by path, but I haven't found one focused on recursively counting files (not listing them or anything else).

Thanks all.

  • 写回答

1条回答 默认 最新

  • douxian1892 2015-06-09 12:36
    关注

    I came across this from the user-contributed section in PHP Manual. Credit to carneiro at isharelife dot com dot br

    function directoryToArray($directory, $recursive = true, $listDirs = false, $listFiles = true, $exclude = '') {
             $arrayItems = array();
             $skipByExclude = false;
             $handle = opendir($directory);
             if ($handle) {
                 while (false !== ($file = readdir($handle))) {
                 preg_match("/(^(([\.]){1,2})$|(\.(svn|git|md))|(Thumbs\.db|\.DS_STORE))$/iu", $file, $skip);
                 if($exclude){
                     preg_match($exclude, $file, $skipByExclude);
                 }
                 if (!$skip && !$skipByExclude) {
                     if (is_dir($directory. '/' . $file)) {
                         if($recursive) {
                             $arrayItems = array_merge($arrayItems, directoryToArray($directory. '/' . $file, $recursive, $listDirs, $listFiles, $exclude));
                         }
                         if($listDirs){
                             $file = $directory . '/' . $file;
                             $arrayItems[] = $file;
                         }
                     } else {
                         if($listFiles){
                             $file = $directory . '/' . $file;
                             $arrayItems[] = $file;
                         }
                     }
                 }
             }
             closedir($handle);
             }
             return $arrayItems;
      }
    

    The function returns complete path listings. Exclusion is by regex passed into the function, and the file count comes from count()ing the returned array.

    This seems to do the job, but I await any alternative answer on this, regarding stuff like code brevity and performance.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大