2012-06-05 109 views
5

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

结构是这样的:

 
dir1/ 
2 files 
    subdir 1/ 
     8 files 

DIR1 = 10个文件

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

DIR2 = 15个文件

我有这个功能,这不因为它只计算最后一个子目录中的文件,并且总数比实际的a多2倍文件的装载。 (将输出80,如果我在最后一个子目录40个文件)

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

回答

6

对于它的乐趣我一起搅打这样的:

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\n"; 

您传递的基本路径,发现处理器和最大目录深度(-1禁用)。找到的处理程序是您在外部定义的函数,它将通过与find()函数中给出的路径相关的路径名称。

希望这是有道理的,并帮助您:)

-3

用于每个回路可以做的伎俩更迅速;-)

我记得,执行opendir从SplFileObject推导类是RecursiveIterator,Traversable,Iterator,SeekableIterator类,因此,如果使用SPL标准PHP库即使在子目录中也可以检索整个图像计数,则不需要一个while循环。

但是,这是一段时间,我没有使用PHP,所以我可能犯了一个错误。

6

你可以使用RecursiveDirectoryIterator

<?php 
function scan_dir($path){ 
    $ite=new RecursiveDirectoryIterator($path); 

    $bytestotal=0; 
    $nbfiles=0; 
    foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) { 
     $filesize=$cur->getSize(); 
     $bytestotal+=$filesize; 
     $nbfiles++; 
     $files[] = $filename; 
    } 

    $bytestotal=number_format($bytestotal); 

    return array('total_files'=>$nbfiles,'total_size'=>$bytestotal,'files'=>$files); 
} 

$files = scan_dir('./'); 

echo "Total: {$files['total_files']} files, {$files['total_size']} bytes\n"; 
//Total: 1195 files, 357,374,878 bytes 
?> 
+0

谢谢劳伦斯!这工作完美:) – Neoweiter

+0

没有probs。很高兴帮助 –

+0

@Neoweiter也扫描子子目录,我以为你只是想要子目录级别? –

0
error_reporting(E_ALL); 

function printTabs($level) 
{ 
    echo "<br/><br/>"; 
    $l = 0; 
    for (; $l < $level; $l++) 
     echo "."; 
} 

function printFileCount($dirName, $init) 
{ 
    $fileCount = 0; 
    $st  = strrpos($dirName, "/"); 
    printTabs($init); 
    echo substr($dirName, $st); 

    $dHandle = opendir($dirName); 
    while (false !== ($subEntity = readdir($dHandle))) 
    { 
     if ($subEntity == "." || $subEntity == "..") 
      continue; 
     if (is_file($dirName . '/' . $subEntity)) 
     { 
      $fileCount++; 
     } 
     else //if(is_dir($dirName.'/'.$subEntity)) 
     { 
      printFileCount($dirName . '/' . $subEntity, $init + 1); 
     } 
    } 
    printTabs($init); 
    echo($fileCount . " files"); 

    return; 
} 

printFileCount("/var/www", 0); 

刚才检查,它的工作不喜欢这样。但是结果的对齐不好,逻辑工作

1

如果有人正在计算文件和目录的总数。

显示/计数总目录和分目录计数

find . -type d -print | wc -l 

显示/计数主要文件的总数和子DIR

find . -type f -print | wc -l 

显示/计数从当前目录中的唯一文件(无子DIR)

find . -maxdepth 1 -type f -print | wc -l 

显示/计数在当前目录总目录和文件(没有子DIR)

ls -1 | wc -l 
相关问题