Home arrow PHP arrow Main Methods of the DirectoryIterator Class in PHP 5

Main Methods of the DirectoryIterator Class in PHP 5

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




During your productive life as PHP developer, you’ve quite possibly found yourself “reinventing the wheel” many times, particularly in those cases where you needed to develop certain modules of an application from scratch. Yeah, I know what this means, since I’ve done the same thing frequently, and it often happens that one doesn’t feel confident enough to delegate the control and programming of the application to third-party software packages.

Even now, when things are radically different (at least to me), since I’m using well-trusted PHP classes, the truth is that developing something that has been already created by someone else happens very often. However, with the introduction of the Standard PHP Library (SPL), hopefully these programming habits will start to change.

Among the numerous and cool features introduced within the SPL package, there’s one that I found especially appealing, because it has become a real time saver in many situations. I’m talking about the set of iterating classes, aimed at traversing different data structures, that is arrays, files, objects and so forth, by using a simple “foreach” language construct.

If you’ve already tasted the power of at least one of these classes, then you know what I mean, because no matter what elements you work with, they can be iterated by the same interface. This is particularly useful when using the “DirectoryIterator” class, which is an important part of the SPL package. Obviously, as its name suggests, this class not only will allow you to traverse any directory by utilizing a regular “foreach” loop, but it will also let you handle information related to the directory in question, by using a few straightforward methods.

Because this class might be handy to use in projects where handling directories is a must, in this new series I’ll be taking a close look at some of its most important methods. We will explore these methods appropriately in conjunction with the corresponding code samples. Therefore, get ready to see the real power of this class. Let’s begin this journey right now!

>>> 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: