note 35712 added to function.opendir
| From: | webmaster at rn2 dot php dot net | Date: | Thu, 11 Sep 2003 20:09:34 +0000 |
| Subject: | note 35712 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-56219@lists.php.net to get a copy of this message | ||
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, "");
?>
----
Manual Page -- http://www.php.net/manual/en/function.opendir.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35712
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35712&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35712&report=yes