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