duanjianlu0506 2011-05-13 10:16
浏览 14
已采纳

删除重复的RecursiveIteratorIterator

I was thinking of using RecursiveIteratorIterator to display the "top-directories", in my example layout and puffar, but I cannot get it to work. The structure is as follows:

| images |
  => layout
       =>lab
  => puffar 

and the result is

layout
layout
layout
layout/lab
layout/lab
puffar
puffar


$directory = 'images';

    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

    while($it->valid()) {

    if (!$it->isDot()) {       
        echo $it->getSubPath() . "
";       
    }

    $it->next();
    }

another thing that I cannot get to work is if I change the directory path to

$directory = 'newsletter.site.se/images';

it is not displaying anything. It would be fantasic if someone could help me. thanks linda

  • 写回答

1条回答 默认 最新

  • dpfad62426 2011-05-13 10:31
    关注

    Do you really need RecursiveDirectoryIterator if you just want to display the top directories?

    Instead you could use:

    <?php
    foreach (new DirectoryIterator('.') as $entry) {
        if (!$entry->isDot() && $entry->isDir()) {
            echo $entry, '<br />';
        }
    }
    

    If you want all directories, try: (for example)

    <?php
    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'), RecursiveIteratorIterator::SELF_FIRST);
    foreach ($it as $entry) {
        if ($entry->isDir()) {
            echo $entry, '<br />';
        }
    }
    

    About your second question, have you checked that the path is correct and readable for php?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求