PHP
  Home arrow PHP arrow Page 2 - Main Methods of 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

Main Methods of the DirectoryIterator Class in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2006-09-11


    Table of Contents:
  • Main Methods of the DirectoryIterator Class in PHP 5
  • Why you should use the DirectoryIterator class: examining a concrete example
  • A standard way to traverse directories: using the DirectoryIterator class
  • A deeper look at the DirectoryIterator class: using the key() and current() methods
  • Resetting a directory’s internal pointer: the rewind() method

  • 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


    Main Methods of the DirectoryIterator Class in PHP 5 - Why you should use the DirectoryIterator class: examining a concrete example
    ( Page 2 of 5 )

    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.



     
     
    >>> 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 5 Hosted by Hostway
    Stay green...Green IT