< Back to Code Samples
<?php
/*  
    CLASS: directory_obj
    WRITTEN: August 2007 by Aaron Hill (AMH)
    PURPOSE: A somewhat generic directory class for making the directory
        scanning a little bit easier. Provides some extra information such
        as filesizes and whatnot. 
*/


class directory_obj {
        
// The directory path
        
private $directory;
        
// The resource handle
        
private $dir_handle;
        
        
// An array of the files contained within
        
private $files;
        
// How many files
        
private $file_count;
        
        
// An array of the names of the subdirectories contained within
        
private $subdirs;
        
// How many subdirectories
        
private $subdir_count;
        
        
// $directory is the path to the directory
        
function __construct($directory){
            if (!
is_dir($directory)) 
            { die(
"ERROR: $directory is not a valid directory."); }
            
            
// Initialize the object variables
            
$this->files = array();
            
$this->file_count 0;
            
$this->subdirs = array();
            
$this->subdir_count 0;
            
            
// Add a slash to the end, if there isn't one already
            
if (substr($directory,-1,1) != "/") { $directory .= "/"; }
            
$this->directory $directory;
            
// use the scandir() MEMBER function (not the PHP5 scandir function)
            
$this->scandir();
        }
        
        
// Garbage collection
        
function __destruct(){
            
closedir($this->dir_handle);
        }
        
        
/**************************
        MEMBER FUNCTION: scandir()
        ARGUMENTS: none
        RETURNS: nothing (sets object variables)
        PURPOSE: Iterates through the given directory handle and 
            sorts out the subdirectories from the files, skips the
            dot twins (. and ..), and keeps a running total of both
            files and subdirectories.
        ***************************/
        
function scandir(){
            
// Create the directory handle
            
$this->dir_handle opendir($this->directory) or 
                die(
"ERROR: Problem opening $directory");
            
            
// Iterate through the directory
            
while (false !== ($filename readdir($this->dir_handle))) {
                
// Skip the dot twins
                
if ($filename == "." || $filename == "..") { continue; }
                
// If it's a directory, append it to the subdirectory list
                
if (is_dir($this->directory $filename)) {
                    
$this->subdirs[++$this->subdir_count] = $filename;
                }
                
// Otherwise if it's a file, append it to the file list.
                
else if (is_file($this->directory $filename)) {
                    
$this->files[++$this->file_count] = $filename;
                }
            }
        }
        
        
/**************************
        MEMBER FUNCTION: name()
        ARGUMENTS: none
        RETURNS: the name of the directory only (NOT the path!)
        PURPOSE: Sometimes you just want the name of the directory without
            the extraneous path information.
        ***************************/
        
function name() {
            
// Start out with the path
            
$name $this->directory;
            
// remove trailing slashes
            
while (substr($name,-1,1) == "/") { $name substr($name,0,-1); }
            
// Extract the name portion -- the text after the last slash
            
$name substr($name,strrpos($name,"/")+1);
            return 
$name;
        }
        
        
// Property accessors
        
function file_count() { return $this->file_count; }
        function 
subdir_count() { return $this->subdir_count; }

        
/**************************
        MEMBER FUNCTION: getFiles("")
        ARGUMENTS: $ext - the extension you're wanting, specifically
        RETURNS: $output -an array of filesizes with the filenames as keys
        PURPOSE: For those times when you want a listing of the files
            held within this directory. You can specify extensions if you
            just want certain files.
        ***************************/
        
function getFiles($ext ""){
            
// the array to be returned
            
$output = array();
            
// Iterate through files
            
foreach ($this->files as $filename) {
                
// If no extension was specified, or the extension matches
                
if ($ext == "" or  strtolower($ext) == strtolower(substr($filename,-strlen($ext))))
                {
                    
// Add the file to the output array. Key: filename  Value: filesize
                    
$output[$filename] = filesize($this->directory $filename);
                }
            }
            return 
$output;
        }
        
        
/**************************
        MEMBER FUNCTION: getSubdirNames()
        ARGUMENTS: none
        RETURNS: $output - an array of the names of the subdirectories
        PURPOSE: Iterates through the subdirectories stored during the constructor
            and returns them all in an array form. This is a bit simpler than the
            getFiles function.
        ***************************/
        
function getSubdirNames(){
            
// No sub directories!
            
if ($this->subdir_count() == 0) return null;

            
// for returning
            
$output = array();
            
            
// Iterate, push each one onto the array
            
foreach($this->subdirs as $subdir) {
                
array_push($output$subdir);
            }
            return 
$output;
        }
        
        
/**************************
        MEMBER FUNCTION: getSubdirs()
        ARGUMENTS: none
        RETURNS: $output - an array of directory_obj objects
        PURPOSE: This function is pretty neat -- it allows for recursive directory
            traversal, but only at the discretion of the program. It iterates through
            each subdirectory and creates a new directory_obj for that subdirectory.
            
        ***************************/
        
function getSubdirs(){ 
            
// No sub directories!
            
if ($this->subdir_count() == 0) return null;

            
// for returning
            
$output = array();
            
            
// Iterate through known subdirectories and instantiate
            // a new directory object for each.
            
foreach ($this->subdirs as $subdir) {
                
$output[$subdir] = new directory_obj($this->directory $subdir);
            }
            return 
$output;
        }
}

?>