PHP
  Home arrow PHP arrow Page 4 - Main Methods of the DirectoryIterator ...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Main Methods of the DirectoryIterator Class in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-09-11

    Table of Contents:
  • Main Methods of the DirectoryIterator Class in PHP 5
  • Why you should use the DirectoryIterator class: examining a concrete example
  • A standard way to traverse directories: using the DirectoryIterator class
  • A deeper look at the DirectoryIterator class: using the key() and current() methods
  • Resetting a directory’s internal pointer: the rewind() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Main Methods of the DirectoryIterator Class in PHP 5 - A deeper look at the DirectoryIterator class: using the key() and current() methods


    (Page 4 of 5 )

    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


       · If you're used to work intensively processing directories, then you'll find the...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT