PHP
  Home arrow PHP arrow Page 5 - 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? 
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 - Using the getPath() and getPathName() methods
    ( Page 5 of 5 )

    As I mentioned before, the last two methods that I’ll cover in this tutorial are “getPath()” and “getPathName().” The first one, as the name suggests, returns the path of the selected directory, while the second one retrieves the path and filename of the current directory element.

    Regarding the first method, here’s an example of how to use it. Check it out:

    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        if($dirContent->isFile()){
            echo 'Path of current file is : '.$dirContent->getPath
    ().'<br />';
        }
        else{
            echo 'Path of current directory is : '.$dirContent-
    >getPath().'<br />';
        }
    }

    As you can see, whether the current element being traversed is a file or a directory, the value returned by the “getPath()” method is the same. This fact is clearly demonstrated by the below results:

    Path of current directory is : default_path
    Path of current directory is : default_path
    Path of current file is : default_path
    Path of current file is : default_path

    Now that you understand how the “getPath()” method works, turn your attention to the next one, that is “getPathName(),” which returns slightly more detailed information about the current directory entry. A possible implementation for this method can be seen below:

    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        if($dirContent->isFile()){
            echo 'Path of current file is : '.$dirContent-
    >getPathName().'<br />';
        }
        else{
            echo 'Path of current directory is : '.$dirContent-
    >getPathName().'<br />';
        }
    }

    Logically, the previous script outputs the following values for each directory element:

    Path of current directory is : default_path/.
    Path of current directory is : default_path/..
    Path of current file is : default_path/file1.txt
    Path of current file is : default_path/file2.txt

    Okay, after covering some of the most relevant methods included with the “DirectoryIterator” class, probably you’ll have realized its awesome capabilities for dealing with timestamps, paths and index nodes. If you’re currently developing PHP applications that do a lot of work with directories, then consider including this class in your toolbox.

    To wrap up

    In this second article of the series, I took an in-depth look at some helpful methods packaged with the “DirectoryIterator” class that can help you obtain information on timestamps, paths and index nodes of directory entries

    However, there’s a few more methods worth covering with reference to this class in particular, thus in the last article, I’ll be explaining them, of course accompanied by copious hands-on examples. Meet you in the last part!



     
     
    >>> 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 2 Hosted by Hostway
    Stay green...Green IT