HomePHP 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.
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.