PHP
  Home arrow PHP arrow Page 3 - 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 standard way to traverse directories: using the DirectoryIterator class


    (Page 3 of 5 )

    As I said right at the beginning of this article, the “DirectoryIterator” class bundled with the SPL package allows you to traverse any directory by using  a typical “foreach” loop. Now, keeping in mind the example that you saw in the prior section, have a look at the following one, which shows why the “DirectoryIterator” class can be a real time saver:

    // traverse directory with 'DirectoryIterator' class
    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
       
    echo $dirContent.'<br />';
    }

    Did you think that traversing directories with this class was much harder? Not at all! As you can appreciate, first I created an instance of the class, and passed to its constructor the path of the directory being read. Next, I used a simple “foreach” loop, handy for reading the respective contents.

    Obviously, this short code snippet outputs the same results that you saw in the previous section. They’re as follows:

    .
    ..
    file1.txt
    file2.txt

    Wasn’t that great? With a few lines of code you get the same results, and all without having to code a custom class. However, this is only a humble beginning, since the “DirectoryIterator” class features plenty of useful methods, which can be used for processing directories in all sorts of clever ways.

    Since you have now realized the capabilities that come with the “DirectoryIterator” class, in the next few lines I’ll provide you with some helpful pointers on how to use some of its most significant methods. Therefore, please read the following section.

    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 6 hosted by Hostway
    Stay green...Green IT