
Mihkel Putrinsh - 2006-07-07 08:34:01 -
In reply to message 1 from Jon Mosier
you might modify the class as follows:
that should do the trick.
there, under rootTreeNode object, the directory structure can be accessed
----------------------
<?php
//=========================================================
// class "DeepDir" take files from all nested directories
// Using:
//
// $dirName = '..';
// $dir = new DeepDir();
// $dir->setDir( $dirName );
// $dir->load();
// foreach( $dir->files as $pathToFile ){
// echo $pathToFile."\n";
// }
//
// 07.07.2006 Mihkel-Mikelis Putrinsh
// added treeNode class functionality
class DeepDir{
var $dir;
var $files;
function DeepDir(){
$this->dir = '';
$this->files = array();
$this->dirFILO = new FILO();
}
function setDir( $dir ){
$this->dir = $dir;
$this->files = array();
$this->dirFILO->zero();
$this->rootTreeNode =& new treeNode( $this->dir, true );
$this->dirFILO->push( array( 'treeNode'=>$this->rootTreeNode, 'path'=>$this->dir ) );
}
function load(){
while( $curElem = $this->dirFILO->pop() ){
$this->curDir = $curElem['path'];
$this->curTreeNode =& $curElem['treeNode'];
$this->loadFromCurDir();
}
}
function loadFromCurDir(){
if ( $handle = @opendir( $this->curDir ) ){
while ( false !== ( $file = readdir( $handle ) ) ){
if ( $file == "." || $file == ".." ) continue;
$filePath = $this->curDir . '/' . $file;
$fileType = filetype( $filePath );
if ( $fileType == 'dir' ){
$treeNode =& new treeNode( $file, true );
$this->curTreeNode->appendChild( $treeNode );
$this->dirFILO->push( array( 'treeNode'=>$treeNode, 'path'=>$filePath ) );
continue;
}
$this->files[] = $filePath;
$treeNode =& new treeNode( $file, false );
$this->curTreeNode->appendChild( $treeNode );
}
closedir( $handle );
}
else{
echo 'error open dir "'.$this->curDir.'"';
}
}
} // end class
//================================
// directory tree nodes
//
class treeNode{
var $isDir = false;
var $isFile = false;
var $filename;
var $parentNode;
var $childs = array();
function treenode($filename, $isDir){
$this->filename = $filename;
$this->isDir = $isDir;
$this->isFile = !$isDir;
}
function appendChild(&$childNode){
$this->childs[] =& $childNode;
$childNode->parentNode =& $this;
}
} // end class treeNode
//================================
// stack: First In Last Out
//
class FILO{
var $elements;
function FILO(){
$this->zero();
}
function push( $elm ){
array_push( $this->elements, $elm );
}
function pop(){
return array_pop( $this->elements );
}
function zero(){
$this->elements = array();
}
} // end class FILO
?>
---------------------