HoboBen HoboBen - 1 month ago 12
PHP Question

PHP readdir is not reading some files

I'm using the following code to loop through a directory to print out the names of the files. However, not all of the files are displayed. I have tried using clearstatcache with no effect.

$str = '';
$ignore = array('.', '..');

$dh = @opendir( $path );
if ($dh === FALSE)
{
// error
}

$file = readdir( $dh );
while( $file !== FALSE )
{
if (in_array($file, $ignore, TRUE)) { break; }
$str .= $file."\n";
$file = readdir( $dh );
}


Here's the contents of the directory right now:

root.auth test1.auth test2.auth test3.auth test5.auth


However, test5.auth does not appear. If I rename it to test4.auth it does not appear. If I rename it to test6.auth it does appear. This is reliable behaviour - I can rename it several times and it still won't show up unless I rename it to test6.auth.

What on earth could be happening?

I'm running Arch Linux (kernel 2.6.26-ARCH) with PHP Version 5.2.6 and Apache/2.2.9 with Suhosin-Patch. My filesystem is ext3 and I'm running fam 2.6.10.

Answer

Your break keywords messes up your code:
Your loop very likely first encounters the '.' directory and than breaks out of your while loop.

try replacing it with a continue and you should be fine.

Comments