Bluehost.com Web Hosting $6.95

Directory Recursion and Multidimensional Arrays

This is a discussion on Directory Recursion and Multidimensional Arrays within the PHP Language forums, part of the PHP Programming Forums category; I have created a script which recurses a display of directories like so: <? $dir = "/path/to/base/directory&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-25-2006
Tyrone Slothrop
 
Posts: n/a
Default Directory Recursion and Multidimensional Arrays

I have created a script which recurses a display of directories like
so:

<?
$dir = "/path/to/base/directory";
function scan_dir_recurse ($dir,$tab)
{
global $fileArr;
$tab++;
if ($tab > 4) { exit ("Tab length exceeded."); }
$files = scandir($dir);
for ($i=0; $i<count($files); $i++)
{
if ($files[$i] != '.' && $files[$i] != '..')
{
echo str_repeat(" ", $tab-1).$files[$i]."\n";
if (is_dir($dir.'/'.$files[$i]))
{
scan_dir_recurse ($dir.'/'.$files[$i],$tab);
}
}
}
}
scan_dir_recurse($dir,0);
?>

What is want to do is generate a multidimensional array which will be
accessable on a DHTML page of select boxes where, a user first selects
a top directory, then a select box appears of directories under it,
etc. until a list of available files is available.

For example:
$fileArr[0] = 'dir1';
$fileArr[0][0] = 'dir2';
$fileArr[0][0][0] = 'dir3';
$fileArr[0][0][0][0] = 'a_file';

The solution to adding another level of recursion to the array escapes
me at the moment. Any ideas?

TIA!
Reply With Quote
  #2 (permalink)  
Old 09-26-2006
pittendrigh
 
Posts: n/a
Default Re: Directory Recursion and Multidimensional Arrays


> For example:
> $fileArr[0] = 'dir1';
> $fileArr[0][0] = 'dir2';
> $fileArr[0][0][0] = 'dir3';
> $fileArr[0][0][0][0] = 'a_file';
>


Make a tree structure instead.......

Reply With Quote
  #3 (permalink)  
Old 09-26-2006
Oli Filth
 
Posts: n/a
Default Re: Directory Recursion and Multidimensional Arrays

Tyrone Slothrop said the following on 25/09/2006 19:24:
> I have created a script which recurses a display of directories like
> so:
>
> <?
> $dir = "/path/to/base/directory";
> function scan_dir_recurse ($dir,$tab)
> {
> global $fileArr;
> $tab++;
> if ($tab > 4) { exit ("Tab length exceeded."); }
> $files = scandir($dir);
> for ($i=0; $i<count($files); $i++)
> {
> if ($files[$i] != '.' && $files[$i] != '..')
> {
> echo str_repeat(" ", $tab-1).$files[$i]."\n";
> if (is_dir($dir.'/'.$files[$i]))
> {
> scan_dir_recurse ($dir.'/'.$files[$i],$tab);
> }
> }
> }
> }
> scan_dir_recurse($dir,0);
> ?>
>
> What is want to do is generate a multidimensional array which will be
> accessable on a DHTML page of select boxes where, a user first selects
> a top directory, then a select box appears of directories under it,
> etc. until a list of available files is available.
>
> For example:
> $fileArr[0] = 'dir1';
> $fileArr[0][0] = 'dir2';
> $fileArr[0][0][0] = 'dir3';
> $fileArr[0][0][0][0] = 'a_file';
>
> The solution to adding another level of recursion to the array escapes
> me at the moment. Any ideas?


You can't have it exactly as you demonstrate above, because that would
mean that $fileArr[0] would have to be a string ('dir1') and an array
(the sub-array) simultaneously. A couple of suggestions:

1. In each array (or sub-array), have $array[0] be the directory name,
and then $array[1], $array[2], ... be the sub-arrays.

2. Have each element of the array be a class object, e.g.:

class Directory
{
public $name;
public $contents;
}

where $contents is an array of child Directory objects.


As for implementation, you will need to add an extra argument to the
scan_dir_recurse() function, which you will use to pass the current
parent directory object. Then scan_dir_recurse can add child objects to
it as it finds them.


--
Oli
Reply With Quote
  #4 (permalink)  
Old 09-27-2006
Tyrone Slothrop
 
Posts: n/a
Default Re: Directory Recursion and Multidimensional Arrays

On Tue, 26 Sep 2006 15:03:24 GMT, Oli Filth <catch@olifilth.co.uk>
wrote:

>1. In each array (or sub-array), have $array[0] be the directory name,
>and then $array[1], $array[2], ... be the sub-arrays.
>
>2. Have each element of the array be a class object, e.g.:
>
> class Directory
> {
> public $name;
> public $contents;
> }
>
>where $contents is an array of child Directory objects.
>
>
>As for implementation, you will need to add an extra argument to the
>scan_dir_recurse() function, which you will use to pass the current
>parent directory object. Then scan_dir_recurse can add child objects to
>it as it finds them.


After making myself crazy with this, I think AJAX can do what I need
quite well, particurly since the directory structure is four deep and
there are duplicate directory and file names. The first select will
show the first set of directories. onSelect will generate and display
the next set of directories from a call to the script, etc. The only
gotcha is that the script is going to have to know what is a file and
what is a directory.

It ought to be an interesting application. ;-)

Thanks!

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:27 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0