PHP
  Home arrow PHP arrow Page 5 - 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 - Resetting a directory’s internal pointer: the rewind() method


    (Page 5 of 5 )

    I’m pretty sure that you’ve realized the great functionality provided by the “DirectoryIterator” class. Nevertheless, I’d like you to learn an additional method that can be really useful when working specifically with directories.

    Since the code samples that I wrote before showed how easy it is to work with directories that can be handled as array elements, this new example demonstrates how to use the handy “rewind()” method, which wraps the overall functionality exposed by the “DirectoryIterator” class. Take a look at the following source code:

    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        echo 'Value of current element is as follows : '.$dirContent->current().'<br />';
    }
    // reset directory pointer by 'rewind()' method
    $dirProc->rewind();
    // display first element of directory
    echo 'First element in directory is the following : '.$dirProc->current();

    As the above example illustrates, the “rewind()” method behaves nearly identical to the “reset()” function that you’ve probably used hundreds of times when processing regular arrays. In this case, after iterating over the specified directory, the script resets the internal pointer, and finally displays the first element of that directory. This condition is clearly reflected by the following output:

    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
    First element in directory is the following : .

    All right, I guess that this final example gives you a good idea of how the “DirectoryIterator” class can be used to traverse a particular directory. As homework, I suggest you to tweak all the code samples shown here and introduce your own improvements. Fun is guaranteed!

    Wrapping up

    That’s all for now. In this first article of the series, I covered some of the most relevant methods that come with the “DirectoryIterator” class, in this way demonstrating how easy is to traverse any specified directory with a regular “foreach” loop.

    But this story isn’t over yet. In the next article, I’ll be reviewing other cool methods, which can be very convenient for obtaining information such as size, path, and files contained inside a particular directory. I'll see you in the next part!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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