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

Main Methods of the DirectoryIterator Class in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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


       · If you're used to work intensively processing directories, then you'll find the...
     

       

    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 6 hosted by Hostway
    Stay green...Green IT