需要在php中搜索已搜索文件的数量

时间:2014-08-12 04:18:34

标签: php

我试图计算一个物体的元素。 我的代码:

$myfiles = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator($dir));   
$array[] = $myfiles;

echo'<font size="4" style="font-family:Verdana,Tahoma,Arial,Helvetica,sans-serif;">Files Found:   </font>';
echo'<font size="4" style="font-family:Verdana,Tahoma,Arial,Helvetica,sans-serif;">';
foreach($array as $f)
    {               
        $c[] = $f;
        echo count($c);
    }
 echo'</font>';
 echo '<hr>';

此代码输出结果,因为对于4个元素,计数显示为1。

请问我能告诉我哪里出错了。

1 个答案:

答案 0 :(得分:1)

你的代码毫无意义。如果你想使用这些迭代器计算文件,那么你可以这样做:

// Create a recursive directory iterator which skips
// the current and parent directories (i.e. "." and "..")
$rDirIter = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);

// Create an iterator for the recursive iterator
$files = new RecursiveIteratorIterator($rDirIter);

// Counter used to count how many files there are
$count = 0;

// Iterate over all the files in the iterator
foreach ($files as $file) {

    // Uncomment to see files...
    //var_dump((string) $file);

    // Increment counter for each encountered file
    $count++;
}
// Echo the count
echo $count;

您应该查看RecursiveDirectoryIteratorRecursiveIteratorIterator迭代器的文档。