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.
Next: The getPerms() and getType() methods >>
More PHP Articles
More By Alejandro Gervasio