限制 RecursiveIteratorIterator() 递归深度

发布于 2024-09-06 12:29:19 字数 447 浏览 3 评论 0原文

我正在使用 RecursiveIteratorIterator() 来探索当前路径的子文件夹,但通过这种方式,所有树都被探索,而不是我只想探索我的 fodler 的直接子文件夹以查找文件。我该如何对 RecursiveIteratorIterator() 说不要走得更远并停止到当前文件夹的第一个子文件夹?

按照 Lauri Lehtinen 的说法,我尝试过以下操作:

    $it = new RecursiveDirectoryIterator("$directory");
    $it->setMaxDepth(1);

我有 PHP 5.2.3,但它告诉我 setMaxDepth 未定义。

致命错误:调用未定义的方法 RecursiveDirectoryIterator::setMaxDepth()

i'm using RecursiveIteratorIterator() to explore subfolders of my current path, but in this way all trhe tree is explored, instead i want just the direct childern of my fodler to be explored for files. How can i say to RecursiveIteratorIterator() to not go much further and stop to the first subfodlers of the current folder?

Following Lauri Lehtinen, i've tried this:

    $it = new RecursiveDirectoryIterator("$directory");
    $it->setMaxDepth(1);

i have PHP 5.2.3 but it say me that setMaxDepth is undefined.

Fatal error: Call to undefined method RecursiveDirectoryIterator::setMaxDepth()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三岁铭 2024-09-13 12:29:19

使用 RecursiveIteratorIterator::setMaxDepth 方法将深度设置为 1 (或任意级别):

Use the RecursiveIteratorIterator::setMaxDepth method to set your depth to 1 (or as many levels as you wish):

烟燃烟灭 2024-09-13 12:29:19

正如 Lauri 所说,将 RecursiveIteratorIterator::setMaxDepth 设置为 1 是最好的方法。

看看 http://www.php.net/manual/ en/class.recursiveiteratoriterator.php#91519 了解以下如何使用它的示例:

<?php
$directory = "/tmp/";
$fileSPLObjects =  new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($directory),
                RecursiveIteratorIterator::CHILD_FIRST
            );
try {
    foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) {
        print $fullFileName . " " . $fileSPLObject->getFilename() . "\n";
    }
}
catch (UnexpectedValueException $e) {
    printf("Directory [%s] contained a directory we can not recurse into", $directory);
}
?>

如果您遇到问题,请确保您使用的是 PHP 5.1.0 或更高版本。

As Lauri said, setting RecursiveIteratorIterator::setMaxDepth to 1 is the best approach.

Have a look at http://www.php.net/manual/en/class.recursiveiteratoriterator.php#91519 for the following example of how to use it:

<?php
$directory = "/tmp/";
$fileSPLObjects =  new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($directory),
                RecursiveIteratorIterator::CHILD_FIRST
            );
try {
    foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) {
        print $fullFileName . " " . $fileSPLObject->getFilename() . "\n";
    }
}
catch (UnexpectedValueException $e) {
    printf("Directory [%s] contained a directory we can not recurse into", $directory);
}
?>

If you're having problems, be sure you're using PHP 5.1.0 or later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文