Home arrow PHP arrow Page 4 - Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP

Using the getMTime(), getATime(), and getCTime() methods - PHP

Are you interested in having at your disposal a quick reference for working with the “DirectoryIterator” class that comes with PHP 5? Then this might be the article that you’ve been waiting for! Welcome to the second tutorial of the series “A Close Look at the DirectoryIterator Class in PHP 5.” Over the course of this set of installments, you’ll find complete coverage of the most important methods bundled with this class, and learn how to take advantage of their excellent functionality.

TABLE OF CONTENTS:
  1. Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
  2. Learning the rewind(), current() and valid() methods
  3. Using the getFileName() and isFile() methods
  4. Using the getMTime(), getATime(), and getCTime() methods
  5. Using the getPath() and getPathName() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
September 18, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: