Login   Register  
PHP Classes
elePHPant
Icontem

File: class.dir.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Ryan Flynn  >  PHP_Dir  >  class.dir.php  >  Download  
File: class.dir.php
Role: ???
Content type: text/plain
Description: class, documentation & example
Class: PHP_Dir
Author: By
Last change:
Date: 2001-06-25 15:34
Size: 5,216 bytes
 

Contents

Class file image Download
<?php
/*
    PHP_Dir
    Version 0.5
    Ryan Flynn (ryan@ryanflynn || DALnet->#php->pizza_milkshake)
    Monday, June 25 2001
    
    This is a class to simplify the listing of classes and allows for commandline-like
    wildcards (i.e. '*.txt'). The class currently returns arrays of matching files or directories
    at the user-specified path.
    
    This class has been tested with PHP 4.0.5 on Windows 2k/IIS. I can't guarentee anything,
    but will act upon any bug reports you send me.
    
    Examples:
    
    ================================================================
    
    This example returns all *.txt files in c:/
    
    require("class.dir.php");
    $d=new PHP_Dir();               //create a new PHP_Dir object
    $d->set_mask("*.txt");          //set the default mask to '*.txt'
    $files=$d->list_files("c:/");   //change this if you're a *nix or Linux user
    //  the above two lines could have been put into one like so:
    //   $files=$d->list_dirs("c:/", "*.txt");
    //    
    //    the ->set_mask() method allows you to alter the mask without
    //    creating a new PHP_Dir object

    //print out a list of files returned matching "c:/*.txt"
    for($i=0;$i<sizeof($files);$i++{
        echo $files[$i]."<br>\n";
    }
    
    // BTW, both file and directory matching use the same variable set by
    // ->set_mask(), so make sure to change it if you're using the same PHP_Dir to
    // list dirs and files in the same page
    ================================================================
    
    Todo:
        I don't have alot of everyday-use experience on Linux... should I add change
            if(is_file($file)) to: if(is_file($file) || is_link($file)) ?
            I'm not sure... waiting on your feedback on this one
            If not, I may simply add a ->list_links() method
        
        Add option in ->list_dirs() to traverse n directory levels
        Maybe return a multi-dimensional array with file size, etc... depends what feedback I get
    
    Note:
        ALWAYS INCLUDE TRAILING SLASH IN DIR, i.e. "c:/", "c:\\" or "/usr/bin/"
        Please email me with any question, comments, suggestions or bug reports.
*/


    class PHP_Dir{
    
        var $mask='';
    
            function PHP_Dir(){}
            
        //exterior
        
            function set_mask($mask=''){
                $this->mask=$mask;
            }
        
            function list_files($dir='/', $mask=''){
            
            /*
                This function returns a list of files from directory
                $dir whose name matches $mask (default is '*')
                
                $dir = directory to list files from
                NOTE: ALWAYS INCLUDE TRAILING SLASH IN DIR, i.e. "c:/", "c:\\" or "/usr/bin/"
                
                $mask = optional mask of files to return, i.e.
                    *.txt       all text files
                    a*          all files that begin with 'a'
                    a*.*        all files that begin with 'a' and have an extension
            */
            
                $return = array();
                
                if(!$mask){
                    $mask=$this->mask;
                }
                
                if(!file_exists($dir)){
                    echo "PHP_Dir: Directory does not exist";
                    return $return;
                }
                
                $d=opendir($dir) or die("PHP_Dir: Failure opening directory");
                while($file=readdir($d)){
                    if(is_file($dir.$file) && $this->matches_mask($file, $mask))
                        $return[sizeof($return)]=$file;
                }
                
                sort($return);
                return $return;
            }
            
            function list_dirs($dir, $mask=''){
            
            /*
                Works exactly the same as ->list_files(), except of course,
                on directories.
            */
            
                $return = array();
                
                if(!$mask){
                    $mask=$this->mask;
                }
                
                if(!file_exists($dir)){
                    echo "PHP_Dir: Directory does not exist";
                    return $return;
                }
                
                $d=opendir($dir) or die("PHP_Dir: Failure opening directory");
                while($file=readdir($d)){
                    if(is_dir($dir.$file) && $this->matches_mask($file, $mask))
                        $return[sizeof($return)]=$file;
                }
                
                sort($return);
                return $return;
            }
        
        //interior
        
            function matches_mask($file, $mask){
            
                $mask = str_replace(".", "\.", $mask);
                $mask = str_replace("*", "(.*)", $mask);
                
                if(eregi("^$mask", $file, $blah))
                    return true;
            }
        
        }
    
?>