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
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.