My readdir in a while statment doesn't work.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3dron
Forum Commoner
Posts: 40
Joined: Sat Feb 01, 2003 10:25 pm

My readdir in a while statment doesn't work.

Post by 3dron »

<?php do {
........mySQL code here calling up a table row according to $pkcount.......
while(($file = readdir($dir)) !== false) {
if ($Player['pick'.$pkcount].'.jpg' == $file)
{$iconpic = $Player['pick'.$pkcount].'.jpg'; closedir($dir); break;}
else {$iconpic = "non.gif";}
}
............more code here, including img src=$iconpic.........
++$pkcount;
} while ($pkcount < 21); ?>

This works if my outter do/while statement only runs once. But if I set the do/while to anything higher the php just sits.

Is it valid to use a closedir and then loop around and "reddir" the same directory?

I have approx. 400 small icon files in a directory. I am trying to check if there is no icon match available it sets the current $iconpic = non.gif, right before an img src=$iconpic right outside of the "while"

I am looking for the most efficeint way to search each of 20 names in a database against 400 filenames in a directory. This could result in approx. 8000 file name reads. Is this partical? Or too hig?
3dron
Forum Commoner
Posts: 40
Joined: Sat Feb 01, 2003 10:25 pm

Simplified version of problem.

Post by 3dron »

<?php
$pkcount = 1;
$dir = opendir("iconfaces");
do {
while(($file = readdir($dir)) !== false) {
echo $file;
}
++$pkcount;
} while ($pkcount < 3); ?>

Why won't this echo out all the file names more than once?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it should but only once
take a look at http://www.php.net/manual/en/function.rewinddir.php
and it might be faster to store the filenames in an array the first time the script iterates the directory (depending on the amount of files that may cost some memory)
Post Reply