PHP
  Home arrow PHP arrow Page 4 - 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 - Implementing the isExecutable(), isReadable() and isWritable() methods
    ( Page 4 of 5 )

    Perhaps this doesn't happen to you so frequently during the development of an application, but there are times where you need to know if a particular file is executable, or if different users has the proper permissions for reading and writing data to that file.

    In this case, the "DirectoryIterator" class makes performing all these tasks a no-brainer process by using the "isExecutable()", "isReadable()" and "isWritable()" methods respectively. Since I want you to understand how each method works separately, below I coded three consecutive examples that show the functionality brought by each of them:

    // implements 'isExecutable()' method
    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        if($dirContent->isExecutable()){
            echo 'Current element is a executable file and its path
    is the following : '.$dirContent->getPathName().'<br />';
        }
    }
    // displays the following:>
    /*
    Current element is a executable file and its path is the following : default_path/Firefox Setup 1.5.0.2.exe

    */
    // implements 'isReadable()' method
    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        if($dirContent->isReadable()){
            echo 'Current element is readable and its path is as
    follows : '.$dirContent->getPathName().'<br />';
        }
        else{
            echo 'Current element is not readable.<br />';
        }
    }
    // displays the following:
    /*
    Current element is readable and its path is as follows :
    default_path/.
    Current element is readable and its path is as follows :
    default_path/..
    Current element is readable and its path is as follows :
    default_path/file1.txt
    Current element is readable and its path is as follows :
    default_path/file2.txt

    */
    // implements 'isWritable()' method
    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        if($dirContent->isWritable()){
            echo 'Current element is writable and its path is as
    follows : '.$dirContent->getPathName().'<br />';
        }
        else{
            echo 'Current element is not writable and its path is as
    follows : '.$dirContent->getPathName().'<br />';
        }
    }
    // displays the following:
    /*
    Current element is writable and its path is as follows :
    default_path/.
    Current element is writable and its path is as follows :
    default_path/..
    Current element is not writable and its path is as follows :
    default_path/file1.txt
    Current element is writable and its path is as follows :
    default_path/file2.txt
    */

    After listing the three previous examples, I'm sure that you've understood correctly how they work. First the "isExecutable()" method is used to determine whether or not a specific file is executable. In this case, I purposely included Firefox's installer, so the script in question can display the corresponding message when this file is examined.

    Now, with reference to the second case, you'll realize that the script iterates over all the elements of the sample directory and checks to see whether they're readable. In this specific instance, I decided to turn all the directory entries into readable pieces, but as you know, this condition might be easily changed, and assign different permissions to each directory element.

    Finally, the third example shows a straight implementation of the "isWritable()" method, which also traverses all the respective directory elements, and checks to see if each of them have the proper permissions for writing data. As you'll realize, all the methods that I exemplified before are pretty intuitive to implement, therefore you shouldn't have any problems using them.

    Fine, after demonstrating some practical examples of how to utilize the "isExecutable()," "isReadable()" and "isWritable()" methods, which are included with the "DirectoryIterator" class, it's time to jump forward and take a quick look at a couple of additional introspective ones.

    In this case, I'm talking about the "getPerms()" and "getType()" methods, where the first one can be used for obtaining the accessing permissions of a given directory entry, while the second one comes in handy for checking its type.

    Since these methods will be covered in the upcoming section, I recommend that you click on the link below and keep 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 1 Hosted by Hostway
    Stay green...Green IT