PHP Logo

Directory Iteration With DirectoryIterator() In PHP 5

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.

Read the full article The Regular Expression Library

22nd March 2008 - 3 minutes read time

Writing regular expressions can sometimes be a real pain, especially if you are not used to them. Rather than trying for yourself to make a regular expression you might want to think about looking for regular expressions that other people have made. Rather than reinventing the wheel to prove you can do something,using free third party regular expressions can save you a lot of time.

The RegExLib or regular expression library is a great resource for finding any regular expression that you are looking for. The Regular Expression Library

Read the full article