note 69377 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 19:00:26 +0000 |
| Subject: | note 69377 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126507@lists.php.net to get a copy of this message | ||
Note Submitter: rasmus dot neikes at web dot de
----
I needed a function to offer me a list of files to select from. I used the codes from Marijan
Greguric and Rick as a basis and got the following:
<script type="text/JavaScript">
function pokazi(dir){
x=document.getElementById(dir);
if(x.style.display == "none"){
x.style.display = "";
}else{
x.style.display = "none";
}
}
</script>
<?php
function ls($dir){
$handle = opendir($dir);
for(;(false !== ($readdir = readdir($handle)));){
if($readdir != '.' && $readdir != '..'){
$path = $dir.'/'.$readdir;
if(is_dir($path)) $output[$readdir] = $this->ls($path);
if(is_file($path)) $output[] = $readdir;
}
}
closedir($handle);
return isset($output)?$output:false;
//closedir($handle);
}
function throwtree ($dir, $name, $base='', $id='', $return = '') {
foreach ($dir as $key=>$node) {
$return .= "<dl style=\"border: none; margin:2px;\">";
if (is_array ($node)) {
$return .= "<dt><a href=\"#\"
onClick=\"pokazi('$base$key');return false;\">$key</a></dt><dd
style=\" margin-left:20px; display:none;\" id=\"$base$key\">";
$nextID = $base."/".$key;
$nextBase = "$base$key";
$this->throwtree ($node, $name, $nextBase, $nextID, &$return);
$return .= "</dd>";
} else {
$return .= "\n<dt><input name=\"$name\" type=\"radio\"
value=\"$id/$node\"> $node</dt>\n";
}
$return .= "</dl>";
}
return $return;
}
?>
They are currently in a class, so you might have to edit the $this-> in the recursions.
The first function ls still creates an array as in the original; the throwtree function uses that
array and creates nested defintion-lists of the file- and directory-tree. Nodes can still be folded
using JavaScript from Marijan.
The function takes an additional parameter $name from which it generates a list of radio buttons;
one per file listed. The values are the paths and file names of the files.
The HTML-String validates; as of now, the JavaScript doesn't degrate gracefully, though. I
didn't bother to sort to the array before parsing it, either.
I haven't tested it much yet, but so far it looks good.
Use like this:
<?php
$myclass->throwtree ($myclass->ls ('path/to/files'), 'radioValue');
?>