note 80945 added to class.dir

From: Date: Thu, 07 Feb 2008 22:45:43 +0000
Subject: note 80945 added to class.dir
Groups: php.notes 
Request: Send a blank email to php-notes+get-135884@lists.php.net to get a copy of this message
Here my solution how to do effective recursiv directory listing. Have fun. <?php /** * example of use: */ $d = new RecDir("/etc/",false); echo "Path: " . $d->getRootPath() . "\n"; while (false !== ($entry = $d->read())) { echo $entry."\n"; } $d->close(); class RecDir { protected $currentPath; protected $slash; protected $rootPath; protected $recursiveTree; function __construct($rootPath,$win=false) { switch($win) { case true: $this->slash = '\\'; break; default: $this->slash = '/'; } $this->rootPath = $rootPath; $this->currentPath = $rootPath; $this->recursiveTree = array(dir($this->rootPath)); $this->rewind(); } function __destruct() { $this->close(); } public function close() { while(true === ($d = array_pop($this->recursiveTree))) { $d->close(); } } public function closeChildren() { while(count($this->recursiveTree)>1 && false !== ($d = array_pop($this->recursiveTree))) { $d->close(); return true; } return false; } public function getRootPath() { if(isset($this->rootPath)) { return $this->rootPath; } return false; } public function getCurrentPath() { if(isset($this->currentPath)) { return $this->currentPath; } return false; } public function read() { while(count($this->recursiveTree)>0) { $d = end($this->recursiveTree); if((false !== ($entry = $d->read()))) { if($entry!='.' && $entry!='..') { $path = $d->path.$entry; if(is_file($path)) { return $path; } elseif(is_dir($path.$this->slash)) { $this->currentPath = $path.$this->slash; if($child = @dir($path.$this->slash)) { $this->recursiveTree[] = $child; } } } } else { array_pop($this->recursiveTree)->close(); } } return false; } public function rewind() { $this->closeChildren(); $this->rewindCurrent(); } public function rewindCurrent() { return end($this->recursiveTree)->rewind(); } } ?> ---- Server IP: 217.13.201.10 Probable Submitter: 77.179.243.206 ---- Manual Page -- http://www.php.net/manual/en/class.dir.php Edit -- https://master.php.net/note/edit/80945 Del: integrated -- https://master.php.net/note/delete/80945/integrated Del: useless -- https://master.php.net/note/delete/80945/useless Del: bad code -- https://master.php.net/note/delete/80945/bad+code Del: spam -- https://master.php.net/note/delete/80945/spam Del: non-english -- https://master.php.net/note/delete/80945/non-english Del: in docs -- https://master.php.net/note/delete/80945/in+docs Del: other reasons-- https://master.php.net/note/delete/80945 Reject -- https://master.php.net/note/reject/80945 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135884) next »