PHP
  Home arrow PHP arrow Page 4 - Using Introspective Methods with the D...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This last tutorial of the series is focused mostly on covering all the methods...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT