Is this trying to reproduce a default apache directory list.  Would it be easier to just add a .htaccess to allow directory index?