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

Using the getPath() and getPathName() 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 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
 

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: