26th September 2008 - 4 minutes read time
The normal way of looping through a directory is to get a handle on the directory, then go through each item, making sure that the file is readable and is not "." or ".." before doing something with the file. Because this is done a lot the DirectoryIterator() object was created in PHP5 to simplify this process.
The constructor of the DirectoryIterator() object takes a single parameter, this is the path of the directory that is to be iterated over.
$dir = new DirectoryIterator('/');
To get the current working directory you can use the PHP function getcwd(). This will return the directory that the PHP script is being run from.
$dir = new DirectoryIterator(getcwd());
The previous bit of code will open up the root directory of your server. If you are on a Linux server you will see directories like dev, var, srv, etc, lib, home, root, sbin and usr.