To add to what sleipnir said, you could recursively call a function (but you might want to put a limit on the number of iterations - just as a rule). Every time is_dir() returns true, call the function from within itself. That way, you can create a table each time and indent inside the cell of the parent table to mimic a tree structure.
This is obviously a simple implementation, but it works.