2

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!

Community
  • 1
  • 1
yretuta
  • 7,963
  • 17
  • 80
  • 151
  • 1
    if you just want to list the files, you dont need the treeIterator. The treeIterator is for printing an ASCII tree. – Gordon Feb 27 '12 at 22:45
  • possible duplicate of [ASCII Library for Creating "Pretty" Directory Trees?](http://stackoverflow.com/a/4196368/208809) (remove the `SELF_FIRST` constant and it should list only the files/leaves) – Gordon Feb 27 '12 at 22:46
  • should I rephrase that? I have tried different approaches, the link given and the DirectoryIterator approach. Gordon, is there not any way to retrieve file information from this ASCII tree, or it does just print? – yretuta Feb 27 '12 at 22:48
  • @Ygam Like I said, the treeIterator will print an ASCII tree. So you cannot get at the iterated file in a `foreach`. However, you can use a `for` loop to iterate it and then access the iterated files by accessing the inner Iterator from the treeIterator. – Gordon Feb 27 '12 at 22:53
  • I was asking that so that I can display the files as links that points to the file path – yretuta Feb 27 '12 at 22:54
  • The question remains: do you actually need the ASCII tree or do you just need to display the files as links? – Gordon Feb 27 '12 at 22:57
  • both. I need a tree to quickly navigate through the log directories, then display each log file in the tree as links – yretuta Feb 27 '12 at 23:00
  • Please check whether http://stackoverflow.com/questions/2207599/multidimensional-array-iteration/2207739#2207739 solves your problem (you need to adjust the markup to your needs and use a `RecursiveDirectoryIterator` instead of the `ArrayIterator` of course) – Gordon Feb 27 '12 at 23:02
  • possible duplicate of [ASCII Library for Creating "Pretty" Directory Trees?](http://stackoverflow.com/questions/1581559/ascii-library-for-creating-pretty-directory-trees) – hakre Feb 28 '12 at 01:48

1 Answers1

4

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.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • this is it! had a hard time looking for a way to access file details as the documentation does not say much. – yretuta Feb 27 '12 at 23:24