Forum Moderators: coopster

Message Too Old, No Replies

array within an array?

         

mightymid

7:48 pm on Jan 26, 2005 (gmt 0)

10+ Year Member



Hi all! I'm trying to write a script that reads a given directory and outputs a list of all the subdirectories it contains as well as all the files the subdirectories contain.

Here's what I have:

$dir = $_SERVER['DOCUMENT_ROOT'] . "/foo/foosubdir/";

$handle = opendir($dir);
while (($filename = readdir($handle)) == true) {
$file_array[] = $filename;
}

--------
The above works fine in returning a list of folders found within "foo." The portion below is supposed to then look thru each folder in "foo" and return a list of files each folder contains. Instead it simply searches the current directory (which isn't "foo") and the site's root directory.
---------

foreach($file_array as $item){

$handle_sub = opendir($item);
while (($filename_sub = readdir($handle_sub)) == true) {
$file_arraysub[] = $filename_sub;
}

foreach ($file_arraysub as $itemsub){
print ($itemsub . "<br>");
}
}

Any help would be much appreciated. Thanks!
Mid. <-- php noobie

dreamcatcher

9:40 am on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could be just a path problem. In the first query you are setting a root path to your directory. The second just takes the value of the array from the first query, with no root path.

Have you tried adding a root path to your second query?

Robber

12:26 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Just thinking out loud here but you might need to use closedir in your loop. Perhaps it opens up the first item in the array which Im assuming is . (current directory), because the directory handle isn't closed before moving to next item it only does .

mightymid

7:42 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



dreamcatcher -- You were right. I added the root path to the second portion and that solved the problem.

I'm so embarrassed to keep coming back with noobie questions, but here's another one.

Apparently the elements from the second array are getting returned for each iteration of the first array. In other words, it's returning something like this:

directory

directory
- subdir1

directory
- subdir1
- subdir2

and so on...

I assume this might be related to Robber's comment about closedir. Will closedir get the looping under control? If so, where would it go?

Thanks (again!)
Mid.

dreamcatcher

8:04 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don`t be concerned about what you post, its all part of the learning curve. Everyone makes simple mistakes, even advanced coders.

Have you tried using closedir to see what happens? Are you echoing the values of your first array before you actually use the data in your second array? Can you post us the code that is giving you the results shown?

mightymid

3:05 pm on Jan 28, 2005 (gmt 0)

10+ Year Member



Well, here is the code. I tried closedir -- in several different places, since I'm not sure where it should go -- but it didn't seem to make a difference. The looping problem persisted AND I got an error line that read: "closedir(): supplied argument is not a valid Directory resource."

As far as echoing the values of the array... I must have done that incorrectly too. It's still looping ad nauseum.

Thanks for taking a look at this!

<?php

$dir = $_SERVER['DOCUMENT_ROOT'] . "/foo/foosubdir/";

$handle = opendir($dir);
while (false!== ($filename = readdir($handle))) {
echo $filename;
$file_array[] = $filename;
}

sort($file_array);
reset($file_array);

foreach($file_array as $item){

$handle_sub = opendir($dir . $item);
while (false!== ($filename_sub = readdir($handle_sub))) {
$file_arraysub[] = $filename_sub;
}

foreach ($file_arraysub as $itemsub){
print ($itemsub . "<br>");
}

}

closedir($dir);

?>

geotopolis

5:22 pm on Jan 28, 2005 (gmt 0)

10+ Year Member



readdir returns .(current directory) and ..(parent directory) which makes your results look recursive.

You should only use readdir on directories.

You should use closedir with a the handle returned
from opendir.

<?php

$dir = $_SERVER['DOCUMENT_ROOT'] . "/stuff/";

$handle = opendir($dir);
while (false!== ($filename = readdir($handle))) {
if(eregi("^\.+$",$filename)) continue;

echo "TOP[".$filename."]<br>\n";

if(is_dir($filename)) $file_array[] = $filename;

}
closedir($handle);

sort($file_array);
reset($file_array);

foreach($file_array as $item){

$handle_sub = opendir($dir . $item);
while (false!== ($filename_sub = readdir($handle_sub))) {
if(eregi("^\.+$",$filename_sub)) continue;
$file_arraysub[] = $filename_sub;
}
closedir($handle_sub);

foreach ($file_arraysub as $itemsub){
print ( "$item : " . $itemsub . "<br>");
}

}

?>