HomePHP Page 2 - Main Methods of the DirectoryIterator Class in PHP 5
Why you should use the DirectoryIterator class: examining a concrete example - PHP
The DirectoryIterator class is an important part of the Standard PHP Library (SPL). Among other things, it lets you traverse any specified directory with a regular "foreach" loop. In this first article of a three-part series, some of the most relevant methods that come with the “DirectoryIterator” class are covered.
Before introducing the goodies of the “DirectoryIterator” class, first let me show you an illustrative example that will demonstrate why this class should be used, particularly in those cases where you need to work intensively with directories inside an application.
All right, say you need to read the contents of a given directory and process these results. This sounds like a typical situation that can happen during the development of a Web application. So, considering this case, I’d be able to code the following class, which reads the contents of a particular directory:
class DirectoryProcessor{ private $dirPath; public function __construct($dirPath){ if(!is_dir($dirPath)){ throw new Exception('Invalid directory path!'); } $this->dirPath=$dirPath; } public function fetchContent(){ $output=''; $dp=opendir($this->dirPath); while($file=readdir($dp)){ $output.=$file.'<br />'; } fclose($dp); return $output; } }
As you can see, the class shown above, called “DirectoryProcesor,” is capable of taking up a specific directory path and reading its contents by using its “fetchContent()” method. The class is indeed basic, but it does what I want. An example of its proper utilization can be seen below. Please take a look:
// example for using the “DirectoryProcessor” class try{ $dirProc=new DirectoryProcessor('default_path/'); echo $dirProc->fetchContent(); } catch(Exception $e){ echo $e->getMessage(); exit(); }
The above script shows how to use the class defined previously to read the contents of a hypothetical directory. Now, suppose that you have a sample “default_path” directory, which also contains two text files, called “file1.txt” and “file2.txt” respectively. Based on this condition, the above script would output the following results:
. .. file1.txt file2.txt
As you can see, my “DirectoryIterator” class has done a decent job, since it can read the contents of the mentioned “default_path” directory, as you can see from the results listed above. However, although this class is capable of doing a good job with regard to handling directory contents, the truth is that this process can be performed without the need to code a custom class.
What’s more, data contained in directories can be handled in different ways, which is very convenient when coding an application that will perform these kinds of tasks. But how can all these things be achieved? The “DirectoryIterator” class that comes with the SPL package can fit the most common requirements for traversing directories, therefore in the following section I’ll show you how to use this class, and explore some of its most relevant methods.
To learn more on how this class works, please click on the link that appears below and keep reading.