2 /* This is a php class to Read a directory recursively and executes defined events */
3 /* when each directory element is read */
14 /*** Constructor (executed when we instatiate the class)*/
17 $this->recursive = false;
19 $this->events = array('readDir_dir', 'readDir_file');
20 $this->handlers = array();
23 /*** Set the directory to read* @param string full directory path*/
24 function setPath( $path )
28 $this->_error('The supplied argument, '.$path.', is not a valid directory path!');
36 /*** Set and event handler* @param string event name* @param string event handler function name*/
37 function setEvent( $event, $handler )
39 if (in_array($event, $this->events) !== false)
41 $this->handlers[$event] = $handler;
45 $this->_error('Event Type specified does not exist.');
51 /*** Set if we want to read through sub folders recursively* @param bool TRUE or FALSE*/
52 function readRecursive( $bool = true )
54 $this->recurse = $bool;
57 /*** Read the directory*/
60 if ( !is_dir($this->path) )
62 $this->_error('Directory to read from is invalid.'.'Please use setPath() to defind a valid directory.');
66 // all set, start reading
67 return $this->_read($this->path);
72 if ($dh = opendir($dir))
75 while ($el = readdir($dh))
76 { $path = $dir.'/'.$el;
78 if (is_dir($path) && $el != '.' && $el != '..')
80 if ($this->_trigger('readDir_dir', $path, $el) == -1)
88 // read sub directories recursively
92 elseif (is_file($path))
94 if ($this->_trigger('readDir_file', $path, $el) == -1)
110 $this->_error('Could not open the directory, '.$path);
115 function _trigger($event, $path, $el)
117 if ($this->handlers[$event])
119 if (!function_exists($this->handlers[$event]))
121 $this->_error('User Function, '.$this->handlers[$event].', defined for the event, '.$event.', does not exist');
125 return call_user_func($this->handlers[$event], $path, $el);
129 function _error($txt)
132 $this->errtxt = $txt;
135 /*** View the last error logged*/
138 return $this->errtxt;
141 /*** View the last error number*/
142 function errorCount()
144 return $this->errcount;