douyi9447 2012-02-27 22:41
浏览 28
已采纳

使用PHP RecursiveTreeIterator构建日志读取器

I have this dir structure for logs

logs
-2012
--01
---01.php
---02.php
--02
---20.php
---23.php

I want to be able to use PHP's RecursiveTreeIterator to be able to display a tree having actual php files(not dirs) as links to display the file contents.

Using the first answer in this question as a guide: Sorting files per directory using SPL's DirectoryTreeIterator

I am new to most of PHP 5's SPL so I need some help here. How do I build the tree? Thanks!

  • 写回答

1条回答 默认 最新

  • douren2395 2012-02-27 23:16
    关注

    As an alternative to the links I provided in the comments already:

    you can also extend the TreeIterator's current() method to provide additional markup:

    class LinkedRecursiveTreeIterator extends RecursiveTreeIterator
    {
        public function current()
        {
            return str_replace(
                $this->getInnerIterator()->current(),
                sprintf(
                    '<a href="%1$s">%1$s</a>',
                    $this->getInnerIterator()->current()
                ),
                parent::current()
            );
        }
    }
    
    $treeIterator = new LinkedRecursiveTreeIterator(
        new RecursiveDirectoryIterator('/path/to/dir'),
        LinkedRecursiveTreeIterator::LEAVES_ONLY);
    
    foreach($treeIterator as $val) echo $val, PHP_EOL;
    

    The above will print the regular ASCII tree the TreeIterator prints but will wrap the filename into hyperlinks. Note that $this->getInnerIterator()->current() returns File objects, so you can access any other file properties, like filesize, last modified, etc, as well.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大