Log in

View Full Version : Little help, readdir()



rpflorence
02-08-2008, 04:14 AM
<?

$list = array(); // initialize an array
$showcaseDir = opendir('../showcase/'); // attempt to open directory
while ($f = readdir($showcaseDir)) { // loop the directory
if ($f=='.' or $f=='..' or $f=='.DS_Store') { continue; } // skip this stuff
array_push($list, $f); // slap it into the array
}
closedir($showcaseDir);

foreach($list as $l){
echo '<option value="'.$l.'">'.$l.'</option>';
}

?>

This kicks out what I want (a list of directory names in a <select> box) but it also prints the whole array afterward as just text! What part of that code is causing it to do that?

I only want it to go into the <option> spots.

This is my first attempt with directory fun.

Thanks!

rpflorence
02-08-2008, 05:22 AM
Nevermind!

I had the foreach() in the code I was referencing and then again in the context of the page, so of course it displayed twice!
:o

BUT, I do have this question now:



$list = array();

$showcaseDir = opendir('../../showcase/');
while ($f = readdir($showcaseDir)) {
if ($f=='.' or $f=='..' or $f=='.DS_Store') { continue; }
$t = file_get_contents("absolute/address/$f/name.txt") or die('Could not read name text file!'); // looking for help here
array_push($list, $t);
}


closedir($showcaseDir);

print_r($list);


I can get to push $t into the array with the absolute address. But when I try a relative path I can't get it to read the text file. I know what the path should be but it doesn't work, and neither does any other relative path I've tried (and I've tried plenty!).

Thanks!