Home arrow PHP arrow Page 2 - Using Introspective Methods with the DirectoryIterator Class in PHP 5

The isDir() method - PHP

If you’re one of those PHP developers who want to explore the package of classes that come bundled with the Standard PHP Library (SPL), then fear not, because you’re at the right place. Welcome to the concluding part of the series "A Close Look at the DirectoryIterator Class in PHP 5." In three parts, this series introduces the most important methods attached to this class, and shows you how to use them by mean of extensive hands-on examples.

TABLE OF CONTENTS:
  1. Using Introspective Methods with the DirectoryIterator Class in PHP 5
  2. The isDir() method
  3. Using the isDot() method
  4. Implementing the isExecutable(), isReadable() and isWritable() methods
  5. The getPerms() and getType() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
September 25, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Indeed, one of the most useful methods that comes with the "DirectoryIterator" class is the one called "isDir()." As its name indicates, this method is capable of determining whether a specific entry is a directory or not. Although this ability may look pretty basic at first glance, the truth is that it can be handy in those cases where you need to spot the differences between directories and plain files.

Of course all this theory sounds good, but it's time to see how the "isDir()" method can be used in a concrete case. The example shown below demonstrates a basic implementation:

$dirProc=new DirectoryIterator('default_path/');
foreach($dirProc as $dirContent){
    if($dirContent->isDir()){
        echo 'Current element is a directory and its path is the
following : '.$dirContent->getPathName().'<br />';
    }
    else{
        echo 'Current element is a file and it path is the
following : '.$dirContent->getPathName().'<br />';
}

That's all you need to get the "isDir()" method working. As you can see, the above script shows how to find out whether a directory entry is also a sub directory itself, or a regular file. Based on the sample directory that I used in previous tutorials, the results outputted by the previous example would be the following:

Current element is a directory and its path is the following : default_path/.
Current element is a directory and its path is the following : default_path/..
Current element is a file and it path is the following : default_path/file1.txt
Current element is a file and it path is the following : default_path/file2.txt

The listing shown above demonstrates basically how the prior script works after using the mentioned "isDir()" method. Here, I'd like to highlight the importance of this method; its functionality can be used as the starting point for building more sophisticated applications, for example a recursive directory iterator (even when the SPL package also includes a recursive iterator class).

Well, now you've seen in action one of the introspective methods included with the "DirectoryIterator" class. But it's time to move on and review others which can be also helpful in determining different types of entries present within a specified directory.

Specifically, in the next few lines, I'll take a look at the "isDot()" method, which also uses the introspective capabilities of the "DirectoryIterator" class. If you're interested in how this method works, please click on the link below and continue reading.



 
 
>>> 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: