note 69377 added to function.readdir
| From: | rasmus dot neikes at web dot de | Date: | Sat, 02 Sep 2006 21:38:21 +0000 |
| Subject: | note 69377 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116641@lists.php.net to get a copy of this message | ||
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');
?>
----
Server IP: 217.13.201.10
Probable Submitter: 213.39.136.249
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/69377
Del: integrated -- https://master.php.net/note/delete/69377/integrated
Del: useless -- https://master.php.net/note/delete/69377/useless
Del: bad code -- https://master.php.net/note/delete/69377/bad+code
Del: spam -- https://master.php.net/note/delete/69377/spam
Del: non-english -- https://master.php.net/note/delete/69377/non-english
Del: in docs -- https://master.php.net/note/delete/69377/in+docs
Del: other reasons-- https://master.php.net/note/delete/69377
Reject -- https://master.php.net/note/reject/69377
Search -- https://master.php.net/manage/user-notes.php