PHP
  Home arrow PHP arrow Page 4 - Main Methods of the DirectoryIterator Class in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT