klaaz Posted January 28, 2007 Share Posted January 28, 2007 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? :) Quote Link to comment https://forums.phpfreaks.com/topic/36018-problem-creating-a-file-tree-browser/ Share on other sites More sharing options...
bibby Posted January 28, 2007 Share Posted January 28, 2007 [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. Quote Link to comment https://forums.phpfreaks.com/topic/36018-problem-creating-a-file-tree-browser/#findComment-170878 Share on other sites More sharing options...
bibby Posted January 28, 2007 Share Posted January 28, 2007 amendment.... you're going to need to pass around the full path too.$_SERVER['DOCUMENT_ROOT'] to start out with, and then you'll have to append each level.[code]function readDirectory($dir , $subpath=FALSE);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36018-problem-creating-a-file-tree-browser/#findComment-170879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.