Jump to content

Problem creating a file tree browser


klaaz

Recommended Posts


Hi there,

I am having a problem with my scripting capabilitys. I am trying to write a recursive directory browser wich uses a javascript (great script btw:  [url=http://www.destroydrop.com/javascripts/tree/]dtree[/url]) to display the results. This problem is pure PHP, to get it right I have to feed the javascript like this (simplyfied):

d.add(1,0, main directory1);
d.add(2,1, first subdir from main directory1);
d.add(3,1, second subdir from main directory1);
d.add(4,0, main directory2);
d.add(5,4, first subdir from main directory2);
d.add(6,4, second subdir from main directory2);
d.add(7,6, first subdir from second subdir frommain directory 2);
and so on.

I hope it's obviously that the numbers create the hiërarchie for dtree here.

What I have working so far (displays the first and second level of the directory):

[code]
<div class="dtree">

<script type="text/javascript">
d = new dTree('d');
d.add(0,-1,'Quartz Quality System');

<?
$dir = "testdata/";
if ($handle = opendir($dir))
{
$teller = 1;
$basis = 0;
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if (!is_dir($dir.$file))
{
echo "d.add($teller,$basis,'$file','#');\n";
}
else
{
$basis2 = $teller;
echo "d.add($teller,$basis,'<strong>$file</strong>','#');\n";
$teller++;

if ($handle2 = opendir($dir.$file."/"))
{
while (false !== ($file2 = readdir($handle2)))
{
if ($file2 != "." && $file2 != "..")
{
echo "d.add($teller,$basis2,'$file2','#');\n";
$teller++;
}
}
closedir($handle2);
}
}
$teller++;
}
}
  closedir($handle);
echo " document.write(d);";
}

?>

</script>
</div>

[/code]
Problem with this script is that it does not get all the levels below the selected directory, but instead stops after the second one. I don't want to create each level by hand, because you never know how many levels deep the subdirectories and files will get. I have tried to build in inrecursiveness, but failed. I've read multiple tutorials but they all create array's in PHP, while I need a simple hiërarchie in numbering the javascript rules. Got a full day of research and trying behind me and definitely need some help now...

Somebody genius out there? :)
Link to comment
https://forums.phpfreaks.com/topic/36018-problem-creating-a-file-tree-browser/
Share on other sites

[quote]I don't want to create each level by hand, because you never know how many levels deep the subdirectories[/quote]

Right, so you right a function for it that can recursive onto itself

[code]
function readDirectory($dir)
{
    //blah blah, read files
    while ($file)
    {
        if(is_dir($file))
        readDirectory($file);    <----  recurse!!
    }
    else
    {
      # whatever you do with files
    }
}
[/code]

Call the function once on your target directory, and it should handle any depth.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.