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

A deeper look at the DirectoryIterator class: using the key() and current() methods - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I mentioned in the previous section, the “DirectoryIterator” class has a bunch of handy methods which are really simple to use. To start seeing the actual functionality of this class, I’m going to show you how to use its “key()” method, which displays the key of each element contained inside a specific directory (remember that the directory on its own is evaluated as an array):

$dirProc=new DirectoryIterator('default_path/');
foreach($dirProc as $dirContent){
    echo 'Key of current element is as follows :'.$dirContent-
>key().'<br />';
}

In this case, the above script takes up the “default_path” directory and loops over all its elements. As you can see, the respective “key()” method displays the following results:

Key of current element is as follows : 0
Key of current element is as follows : 1
Key of current element is as follows : 2
Key of current element is as follows : 3

Based on the contents of the directory that I used from the very beginning, the above example shows how to return the key of each element inside the specified directory by utilizing the “key()” method. Simple and efficient, isn’t it?

Now, after proving the functionality provided by the “key()” method, let’s move on and have a look at another useful method. In this case, I’m referencing the “current()” method, which can be used as follows:

$dirProc=new DirectoryIterator('default_path/');
foreach($dirProc as $dirContent){
    echo 'Value of current element is as follows : '.$dirContent->current().'<br />';
}

As you can see, the “current()” method is quite straightforward, since it simply returns the current value of the directory element being traversed. With reference to the results returned by the prior example, the output is as follows:

Value of current element is as follows : .
Value of current element is as follows : ..
Value of current element is as follows : file1.txt
Value of current element is as follows : file2.txt

Now, do you see how a specified directory can be traversed as you’d do with regular array elements?

Okay, at this stage, you hopefully learned how a couple of helpful methods that belong to the “DirectoryIterator” class can be used to iterate over each element, as you’d do with ordinary arrays.

However, I’m not finished yet, because there are still more handy methods to review. Over the course of the following section, I’ll show you how to use an additional method which turn the “DirectoryIterator” class into a valuable piece of code.

To see how this brand new method will be used, all you have to do is click on the link below and continue reading.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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