HomePHP 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.
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!