note 35712 deleted from function.opendir by sniper

From: Date: Sun, 12 Oct 2003 04:23:39 +0000
Subject: note 35712 deleted from function.opendir by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58321@lists.php.net to get a copy of this message
Note Submitter: webmaster AT tim MINUS oppermann DOT com ---- Hi! I've just written a script which lists all files and all subdirectories and each dirs and files of each subdir. This script should work on Win and on Linux systems. <?PHP // if the list should get very long, it's usefull to use flush flush(); // the root of the list $path="./"; // those lines just prints out the headline echo " <FONT style='font-family: Tahoma; font-size: 12pt'> <B>Listing of $path</B> <br> </FONT> <FONT style='font-family: Tahoma; font-size: 8pt'> "; // this is a recursive function which prints out a array and calls itsself if the value is an array. the $root parameter is nesecarry for the relative path to the root dir specified in the $path variable function output($data, $root){ echo "<ul>"; // run through the array foreach($data as $value){ if ( is_array($value)) { echo "<ul>"; output($value, "$root/$last"); echo "</ul>"; continue; } $last=$value; echo "<li><a href='$root/$value'> $value</a></li>"; } echo "</ul></FONT>"; } // this function also works recrusive. function listdirs($dir, $list){ if($handle = opendir($dir)){ while(false !== ($item = readdir($handle))){ // to avoid endless loops filtering u'd better filter "." and ".." if($item=="." or $item=="..") continue; // hides invisible files. you can remove this line if(preg_match("/^\./", $file)) continue; // if the item is not a dir then it must be a file and is added to the return-array if(!is_dir("$dir/$item")){ $list[] = $item; }else{ // the dir name is added $list[]=$item; $list[] = array(); // the returned array of the dir is added here to the array $list[count($list)-1] = listdirs("$dir/$item/", $list[count($list)-1]); } } // Close directory closedir($handle); } return($list); } $data=array(); $data=listdirs($path, $data); output($data, ""); ?>

« previous php.notes (#58321) next »