PHP Classes

Just directories

Recommend this page to a friend!

      DeepDir  >  All threads  >  Just directories  >  (Un) Subscribe thread alerts  
Subject:Just directories
Summary:How do I get just the directories with no file names
Messages:2
Author:Jon Mosier
Date:2005-11-25 13:19:12
Update:2006-07-07 08:34:01
 

 


  1. Just directories   Reply   Report abuse  
Picture of Jon Mosier Jon Mosier - 2005-11-25 13:19:12
Pardon me for asking what seems like it should be so obvious.

How can I return just the directory names with this class?

Thanks,
Jon

  2. Re: Just directories   Reply   Report abuse  
Picture of Mihkel Putrinsh 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

?>
---------------------