Nice little script. The display kinda reminds me of an XLST'd XML file.
Anyways, take a peek at scandir(), I think it might make the code a little simpler and faster to process: http://php.net/manual/en/function.scandir.php
Hope I helped