Home arrow PHP arrow Page 3 - Main Methods of the DirectoryIterator Class in PHP 5

A standard way to traverse directories: using the DirectoryIterator class - PHP

The DirectoryIterator class is an important part of the Standard PHP Library (SPL). Among other things, it lets you traverse any specified directory with a regular "foreach" loop. In this first article of a three-part series, some of the most relevant methods that come with the “DirectoryIterator” class are covered.

  1. Main Methods of the DirectoryIterator Class in PHP 5
  2. Why you should use the DirectoryIterator class: examining a concrete example
  3. A standard way to traverse directories: using the DirectoryIterator class
  4. A deeper look at the DirectoryIterator class: using the key() and current() methods
  5. Resetting a directory’s internal pointer: the rewind() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
September 11, 2006

print this article



As I said right at the beginning of this article, the “DirectoryIterator” class bundled with the SPL package allows you to traverse any directory by using  a typical “foreach” loop. Now, keeping in mind the example that you saw in the prior section, have a look at the following one, which shows why the “DirectoryIterator” class can be a real time saver:

// traverse directory with 'DirectoryIterator' class
$dirProc=new DirectoryIterator('default_path/');
foreach($dirProc as $dirContent){
echo $dirContent.'<br />';

Did you think that traversing directories with this class was much harder? Not at all! As you can appreciate, first I created an instance of the class, and passed to its constructor the path of the directory being read. Next, I used a simple “foreach” loop, handy for reading the respective contents.

Obviously, this short code snippet outputs the same results that you saw in the previous section. They’re as follows:


Wasn’t that great? With a few lines of code you get the same results, and all without having to code a custom class. However, this is only a humble beginning, since the “DirectoryIterator” class features plenty of useful methods, which can be used for processing directories in all sorts of clever ways.

Since you have now realized the capabilities that come with the “DirectoryIterator” class, in the next few lines I’ll provide you with some helpful pointers on how to use some of its most significant methods. Therefore, please read the following section.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: