Reading directory list – new PHP5 way

It has been long time for using opendir, readdir functions or even scandir for reading directory listing from the disk.  Even myself still using it when in need.

Its time to update your snipped library with new PHP5 object oriented code. This is definitely not a new new thing, but still I want to make an article on it to attract more users to use this feature.

Retrieve directory listing

Using new Iterator thing

The good thing here is that $entry is an object and you can use some useful methods like

As well as others like $entry->getOwner(), getPerms, getSize, isWritable

Retrieve recursive directory listing

Another magic feature it is easy to convert code above to do recursive directory listing, look

See more docs on RecursiveDirectoryIterator, it has second parameter to control what is included in final list.

 

2 comments

  1. Hi,

    You might want to filter out the ‘.’ and ‘..’ dirs. Thanks to the isDot() class method it is super easy:

    if ($entry->isDir() && !$entry->isDot())

    King regards

Leave a Reply

Your email address will not be published.