note 42948 deleted from function.readdir by tularis

From: Date: Sun, 13 May 2007 19:05:10 +0000
Subject: note 42948 deleted from function.readdir by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126538@lists.php.net to get a copy of this message
Note Submitter: phpuser at php dot net ---- Simplified recursive directory listing. Returns FolderListing object which contains an array with all the directories and files. <?php class FolderListing { var $newarr = array(); function loop($stack) { if(count($stack) > 0) { $arr = array(); foreach($stack as $key => $value) { array_push($this->newarr, $stack[$key]); if ($dir = @opendir($stack[$key])) { while (($file = readdir($dir)) !== false) { if (($file != '.') && ($file != '..')) { array_push($arr, $stack[$key].'/'.$file); } } } closedir($dir); } $this->loop($arr); } else { $sorted = sort($this->newarr); return($sorted); } } } $start = new FolderListing; $base = array("baseDir"); $start->loop($base); foreach($start->newarr as $value) { echo $value."<br />"; } ?> Add the following code around the array_push line to just check for directories: if(is_dir($stack[$key].'/'.$file)) { } jw5_feedback at comcast dot net 20-May-2004 06:30 Ok you have seen a bunch of them recursive directory functions, but heres mine that loads them all into an array and emdeded arrays that contain all the file names. /*********************************************/ $main_dir = "downloads"; $A_main = Array($main_dir,); $start =& $A_main; $j = 0; function dir_recurse($dir,&$wia) { $i = 1; if($handle = opendir($dir)) { while(false !== ($file = readdir($handle))) { if($file != "." && $file != ".." ) { if(is_dir($dir."/".$file) == true) { $fullpath = $dir."/".$file; array_push($wia,array($file)); dir_recurse($fullpath,$wia[$i]); $i++; } else { $fullpath = $dir."/".$file; array_push($wia,$fullpath); $i++; } } } closedir($handle); } } dir_recurse($main_dir,$start); /********************************************/ wia stands for "where it's at" in relation to being in a directory.

« previous php.notes (#126538) next »