Home arrow PHP arrow Page 2 - Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP

Learning the rewind(), current() and valid() methods - PHP

Are you interested in having at your disposal a quick reference for working with the “DirectoryIterator” class that comes with PHP 5? Then this might be the article that you’ve been waiting for! Welcome to the second tutorial of the series “A Close Look at the DirectoryIterator Class in PHP 5.” Over the course of this set of installments, you’ll find complete coverage of the most important methods bundled with this class, and learn how to take advantage of their excellent functionality.

  1. Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
  2. Learning the rewind(), current() and valid() methods
  3. Using the getFileName() and isFile() methods
  4. Using the getMTime(), getATime(), and getCTime() methods
  5. Using the getPath() and getPathName() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
September 18, 2006

print this article



Indeed, the title of this section is particularly significant, because it suggests concretely the implementation of three new methods that come with the “DirectoryIterator” class. Of course, here I’m talking about the “rewind(),” “current()” and “valid()” methods respectively, which can be fairly useful in those cases where you need to extend the functionality brought by the respective base class.

Basically, these three methods behave as expected inside an iterator class, since they allow one to move across a specific directory as if one were working with regular arrays. But let’s turn away from the boring theory for a moment and see a practical example, which demonstrates the implementation of these three methods in one single class. Please look at the class shown below:

class DirectoryProcessor extends DirectoryIterator{
    private $dirPath;
    public function __construct($dirPath='default_path/'){
    public function rewind(){
    public function current(){
        return 'Path of current element is '.parent::getPath().'
and its size in bytes is '.parent::getSize();
    public function valid(){
            return true;
        return false;

In short, what I’ve done above is derive a subclass from the original “DirectoryIterator” to extend the functionality provided by the respective “current()” method. Even when this example is extremely comprehensive with reference to its source code, it also shows in a nutshell how the “DirectoryIterator” class can be easily expanded, particularly in those situations where you need to implement additional features aside from the existing ones.

Returning to the previously defined subclass, here is a short script that iterates over a sample “default_path” directory, and displays all the paths of the files contained inside of it:

$dirProc=new DirectoryProcessor;
foreach($dirProc as $dirContent){
echo $dirContent.'<br />';

Basically, the above code snippet shows how to use the “DirectoryProcessor” class, which as you learned previously is simply a child class derived from "DirectoryIterator." The pertinent output of this example is shown below:

Path of current element is default_path and its size in bytes is 0
Path of current element is default_path and its size in bytes is 0
Path of current element is default_path and its size in bytes is 31
Path of current element is default_path and its size in bytes is 31

As you can see, demonstrating how the “rewind(),” “current()” and “valid()” methods work together within the same child class is not only a simple process, but it’s also instructive!

Okay, I firmly believe that the example you saw before is pretty easy to understand, therefore let’s move on and continue exploring the methods exposed by the “DirectoryIterator” class. The following section explains precisely how to use the “getFileName()” and “isFile()” methods, so I suggest you keep reading to learn more about them.

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