Abbey Workshop

PHP: Directory Reader Object

The DirReader class is a PHP 5 class use to read the contents of a directory into separate file and directory arrays.

Lines 3 - 33 sets up a main function that runs some simple code to test the class and its functions. (Note: This part of the source file is stylistically very much like a Java program. A personal preference. Creating the main function and calling it first allows me to control program execution. This is by no means the only way to do this.)

Here is the source code. Comments on the DirReader class follow the source.

DirReader.php

   1:<?php 
   2:main();
   3:
   4:function main(){
   5:    //Set Variables
   6:    $testDirName = "test_dir";
   7:    $fileListArray = array();
   8:    $dirListArray = array();
   9:    
  10:    $aDirectory = new DirReader($testDirName);
  11:            
  12:    // Store File List in Array
  13:    $fileListArray = $aDirectory->getFileList();
  14:    $dirListArray = $aDirectory->getDirList();      
  15:    
  16:    echo "<html><body>\n";
  17:    echo "<pre>\n";
  18:
  19:    
  20:    echo "Reading Directory: ".  $aDirectory->getDirPath() ."\n";
  21:    echo "Current Directory: ". getcwd() ."\n";
  22:    
  23:    echo "-- Files in Directory --\n";
  24:    foreach ($fileListArray as $filename) {
  25:        echo "File: $filename\n";
  26:    }
  27:    
  28:    echo "\n-- Sub Directories --\n";
  29:    foreach ($dirListArray as $filename){
  30:        echo "Sub dir: $filename\n";
  31:    }
  32:    echo "</pre></body></html>\n";
  33:}
  34:
  35:class DirReader{
  36:    private $dh; # Directory Handle
  37:    private $basedir; # Base Directory passed to the object
  38:    private $fileNameArray=array();
  39:    private $dirNameArray=array();
  40:    
  41:    function __construct($dirname){
  42:        $this->basedir = $dirname;
  43:        $this->dh = dir($dirname) or die($php_errormsg);
  44:        $this->parseDirectory();
  45:    }
  46:    
  47:    function parseDirectory(){
  48:        $filename = "";
  49:        while (false !== ($filename = $this->dh->read())){
  50:            $fullpath = $this->basedir . '/' . $filename;
  51:            if (is_file($fullpath)){
  52:                array_push($this->fileNameArray, $filename);
  53:            }else{
  54:                array_push($this->dirNameArray, $filename);
  55:            }
  56:        }
  57:    }
  58:
  59:
  60:    // Get all the files in the directory
  61:    function getFileList(){
  62:        return $this->fileNameArray;
  63:    }
  64:
  65:    // Get all the sub directories in the directory
  66:    function getDirList(){
  67:        return $this->dirNameArray;
  68:    }
  69:
  70:    function getDirPath(){
  71:        return $this->dh->path;
  72:    }
  73:}
  74:?>

Notes on the File

The class is defined on lines 35-57. Lines 36-39 define the object properties. The private keyword is new to PHP5. Lines 41-44 initialize variables get the directory passed to it when the object is created. The constructor then calls the parseDirectory function which reads the directory contents into separate file and directory arrays. The code that reads the info, line 49, comes almost directory out of the documentation. This example uses the object oriented approach to reading a directory handle. Line 51 checks if this is a file or directory. Then, the file name gets pushed into the appropriate place. The methods on lines 60 to 72 allow you to retrieve your desired information.