PHP
  Home arrow PHP arrow Page 5 - Using Introspective Methods with the DirectoryIterator Class in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using Introspective Methods with the DirectoryIterator Class in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2006-09-25


    Table of Contents:
  • Using Introspective Methods with the DirectoryIterator Class in PHP 5
  • The isDir() method
  • Using the isDot() method
  • Implementing the isExecutable(), isReadable() and isWritable() methods
  • The getPerms() and getType() methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Introspective Methods with the DirectoryIterator Class in PHP 5 - The getPerms() and getType() methods
    ( Page 5 of 5 )

    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!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek