PHP
  Home arrow PHP arrow Page 2 - Finding Paths, Timestamps and More wit...
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

Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-09-18

    Table of Contents:
  • Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
  • Learning the rewind(), current() and valid() methods
  • Using the getFileName() and isFile() methods
  • Using the getMTime(), getATime(), and getCTime() methods
  • Using the getPath() and getPathName() methods

  • 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


    Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP - Learning the rewind(), current() and valid() methods


    (Page 2 of 5 )

    Indeed, the title of this section is particularly significant, because it suggests concretely the implementation of three new methods that come with the “DirectoryIterator” class. Of course, here I’m talking about the “rewind(),” “current()” and “valid()” methods respectively, which can be fairly useful in those cases where you need to extend the functionality brought by the respective base class.

    Basically, these three methods behave as expected inside an iterator class, since they allow one to move across a specific directory as if one were working with regular arrays. But let’s turn away from the boring theory for a moment and see a practical example, which demonstrates the implementation of these three methods in one single class. Please look at the class shown below:

    class DirectoryProcessor extends DirectoryIterator{
        private $dirPath;
        public function __construct($dirPath='default_path/'){
            parent::__construct($dirPath);
        }
        public function rewind(){
            parent::rewind();
        }
        public function current(){
            return 'Path of current element is '.parent::getPath().'
    and its size in bytes is '.parent::getSize();
        }
        public function valid(){
            if(parent::valid()){
                return true;
            }
            return false;
        }
    }

    In short, what I’ve done above is derive a subclass from the original “DirectoryIterator” to extend the functionality provided by the respective “current()” method. Even when this example is extremely comprehensive with reference to its source code, it also shows in a nutshell how the “DirectoryIterator” class can be easily expanded, particularly in those situations where you need to implement additional features aside from the existing ones.

    Returning to the previously defined subclass, here is a short script that iterates over a sample “default_path” directory, and displays all the paths of the files contained inside of it:

    $dirProc=new DirectoryProcessor;
    foreach($dirProc as $dirContent){
       
    echo $dirContent.'<br />';
    }

    Basically, the above code snippet shows how to use the “DirectoryProcessor” class, which as you learned previously is simply a child class derived from "DirectoryIterator." The pertinent output of this example is shown below:

    Path of current element is default_path and its size in bytes is 0
    Path of current element is default_path and its size in bytes is 0
    Path of current element is default_path and its size in bytes is 31
    Path of current element is default_path and its size in bytes is 31

    As you can see, demonstrating how the “rewind(),” “current()” and “valid()” methods work together within the same child class is not only a simple process, but it’s also instructive!

    Okay, I firmly believe that the example you saw before is pretty easy to understand, therefore let’s move on and continue exploring the methods exposed by the “DirectoryIterator” class. The following section explains precisely how to use the “getFileName()” and “isFile()” methods, so I suggest you keep reading to learn more about them.

    More PHP Articles
    More By Alejandro Gervasio


       · Over this second part of the series, you'll learn a few more useful methods that are...
     

       

    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