PHP
  Home arrow PHP arrow Page 2 - 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? 
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 isDir() method
    ( Page 2 of 5 )

    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT