PHP
  Home arrow PHP arrow Page 4 - 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 - Using the getMTime(), getATime(), and getCTime() methods


    (Page 4 of 5 )

    As you’ll probably agree, a decent directory iterator class wouldn’t be complete if it’s not capable of dealing with timestamps. Fortunately, the “DirectoryIterator” class won’t disappoint you (at least regarding this specific aspect), since it’s been provided with some interesting methods that retrieve information related to different timestamps.

    First, take a look at the “getMTime()” method listed below, which returns the last modification timestamp of a given directory element:

    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        if($dirContent->isFile()){
            echo 'Last modification timestamp of current file is : '.$dirContent->getMTime().'<br />';
        }
    }

    In this case, the “getMTime()” method used above simply returns the last modification timestamp of the current directory element, which displays the following results, based on the two text files that I used in the previous section:

    Last modification timestamp of current file is : 1149698631
    Last modification timestamp of current file is : 1149698651

    All right, now that you know how to use the “getMTime()” method shown above, take a look at the following code. In this case, I’m referencing the handy “getATime()” method, which returns the timestamp of the last access to a given directory element. Its implementation is as follows:

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

    As you can see, the above example illustrates a basic approach for how to use the “getATime()” method that I mentioned earlier. In particular, the script loops over the selected directory and displays the last access timestamps of both directories and files contained within the directory in question.

    Based on the logic applied by the prior example, the outputted results are listed below:

    Last modification timestamp of current directory is : 1150153955
    Last modification timestamp of current directory is : 1150152774
    Last modification timestamp of current file is : 1149698631
    Last modification timestamp of current file is : 1149698651

    As you may have realized, the “getATime()” method that you learned previously is not difficult to grasp, so let me show you another one which can be handy for retrieving the inode timestamp of a given directory element. Its implementation is as follows:

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

    Just in case you’re wondering what the term “inode” means, let me tell you that it’s short for “Index Node,” that is the sector of the hard disk that contains information about a specific directory or file, such as its name, permissions, etc.

    Okay, now that you understand the word "inode," take a moment to examine the output generated by the script listed a few lines above:

    Last inode modification timestamp of current directory is : 1149698503
    Last inode modification timestamp of current directory is : 1149696540
    Last inode modification timestamp of current file is : 1149698631
    Last inode modification timestamp of current file is : 1149698651

    As you’ll realize, after studying all the previous examples, the “DirectoryIterator” class has been provided with a great package of timestamp-related methods that suit the most common requirements of a web application that works intensively with the file system. The process is reduced to selecting the appropriate method that best fits your needs.

    At this point, we’re almost done for now. Nevertheless, there’s still one more section that I’d like you to read before you get too tired, because it’ll show you how to use two concrete methods called “getPath()” and “getPathName()” respectively.

    As you’ll see, they’ll allow you to retrieve the corresponding path of any directory element, so jump straight into the following section. I’ll be there, waiting for you.

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