HomePHP Page 5 - Using Introspective Methods with the DirectoryIterator Class in PHP 5
The getPerms() and getType() methods - 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.
If all the methods that you learned before seemed fairly easy to grasp, then these will be even simpler. First, look at the example below, which shows how to use the "getPerms()" method to obtain the permission codes that correspond to each directory entry:
$dirProc=new DirectoryIterator('default_path/'); foreach($dirProc as $dirContent){ echo 'Current element has the following permissions :'.$dirContent->getPerms().'<br />'; }
In this case, the script shown above loops over the sample "default_path" directory and retrieves all the permission codes that were given to each directory element. After executing this snippet, it displays the following results:
Current element has the following permissions :16895 Current element has the following permissions :16895 Current element has the following permissions :33206 Current element has the following permissions :33206
Due to the simplicity of the "getPerms()" method, allow me to skip over it rapidly, and show you the last example of this tutorial. In this case, I'm referencing directly the "getType()" method, which can be handy in those situations where you need to determine the type of a particular directory entry, that is if it's a directory or a file. Please, look at the sample code below:
$dirProc=new DirectoryIterator('default_path/'); foreach($dirProc as $dirContent){ echo 'Type of current element is the following : '.$dirContent->getType().'<br />'; }
As you can see, the method used above is equivalent to utilizing a combination of the "isDir()" and "isFile()" methods that were opportunely exemplified earlier. Therefore, in accordance with the above script, the following results are displayed:
Type of current element is the following : dir Type of current element is the following : dir Type of current element is the following : file Type of current element is the following : file Type of current element is the following : file
As you'll realize, determining whether a directory entry is a subdirectory or a file is a task that can be easily carried out by the "getType()" method. Use the above script's source code and utilize different directories to see what kind of output you get in each case. I use this technique when I get bored and fun comes up automatically!
Final thoughts
Finally, this series is finished. I hope you found it enjoyable and instructive, particularly if you're starting to discover the power behind the SPL package. Of course, there are many more predefined classes aside from "DirectoryIterator" that can make your life easier when using PHP 5. Therefore, I suggest you to take a look at the PHP manual to learn more about them. See you in the next PHP tutorial!