Please note the fashion in which readdir()'s
return value is checked in the examples below. We are explicitly
testing whether the return value is identical to (equal to and of
the same type as--see Comparison
Operators for more information) FALSE since otherwise,
any directory entry whose name evaluates to FALSE will stop the
loop (e.g. a directory named "0").
<?php
// Note that !== did not exist until 4.0.0-RC2
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
}
?>