PHP
  Home arrow PHP arrow Page 2 - Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
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? 
Google.com  
PHP

Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek