Home arrow PHP arrow Page 4 - Using Introspective Methods with the DirectoryIterator Class in PHP 5

Implementing the isExecutable(), isReadable() and isWritable() 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.

TABLE OF CONTENTS:
  1. Using Introspective Methods with the DirectoryIterator Class in PHP 5
  2. The isDir() method
  3. Using the isDot() method
  4. Implementing the isExecutable(), isReadable() and isWritable() methods
  5. The getPerms() and getType() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
September 25, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: