Forum Moderators: coopster

Message Too Old, No Replies

arrays and directory content

directory listing into array

         

kumarsena

7:59 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



dear friends,

i have been thinking about improving my pic album script by including some sort of paging system. now im awasre there are a few tutorials out there, but i would like to do thi from scrathc just to learn. so...
anyone out there who knows if there is a function thta will read the contents of a folder and write it to an array. or do i have to play around and insert into array using a while loop and readdir()? any input would be appreciated.

thanks
kumar

jatar_k

8:23 pm on Mar 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this, I think it will load the filenames into an array and then echo them to screen, havent tested though

<?  
$mydir = "/path/to/dir/";
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= "." && $entry!= "..") {
$filearr[] = $entry;
}
}
$d->close();
echo "<ul>";
for each ($filearr as $filename) {
echo "<li>"$filename;
}
echo "</ul>";
?>

kumarsena

11:03 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



thanks will ahve ago at it. and post the results...

kumarsena

11:23 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



hey,

the code had a bug but otherwise is working fine.

these two lines (original code) were modified:

for each ($filearr as $filename) {
echo "<li>"$filename;

for some reason the secind line wasnt working, but i couldnt figure out why, just changed it...new code as below

<? $mydir = "mydir";

$d = dir($mydir);
while($entry = $d->read())
{
if ($entry!= "." && $entry!= "..") {
$filearr[] = $entry; }
}

$d->close();
echo "<ul>";
<b>foreach ($filearr as $filename) {</b>

<b> echo $filename;
echo "<br />";</b>

}

echo "</ul>";?>

thanks jatar_k! (i assume its GPL, rite? :) )
hope this is useful some others too...

kumarsena

11:25 pm on Mar 9, 2004 (gmt 0)

10+ Year Member



ok just ignore teh bold tags, just tried to highlite some parts...

jatar_k

2:55 am on Mar 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



oops my fault kumarsena,

echo "<li>"$filename;

should be

echo "<li>",$filename;

I forgot the comma