我正在使用这个脚本,但我想指定递归函数的最大深度.我不知道如何使用它,我总是得到一个错误.我该如何在这里使用:: setMaxDepth?
public function countFiles($path)
{
global $file_count;
$depth=0;
$ite=new RecursiveDirectoryIterator($path);
$file_count=0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) :
$file_count++;
$files[] = $filename;
endforeach;
return $file_count;
}
Run Code Online (Sandbox Code Playgroud)
你需要调用setMaxDepth()一个实例RecursiveIteratorIterator.当您RecursiveIteratorIterator在foreach语句中构造时,这很困难.相反,使用变量来保存它.
$files = new RecursiveIteratorIterator($ite);
$files->setMaxDepth($depth);
foreach ($files as $filename => $cur) {
$file_count++;
$files_list[] = $filename;
}
Run Code Online (Sandbox Code Playgroud)
但请注意,此处不需要使用循环(除非您的代码执行上面示例中已删除的其他内容).你可以获得文件的数量iterator_count().
function countFiles($path)
{
$depth = 1;
$ite = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($ite);
$files->setMaxDepth($depth);
return iterator_count($files);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1483 次 |
| 最近记录: |