What are the potential pitfalls when using readdir function in PHP?

One potential pitfall when using the readdir function in PHP is that it may return false when there are no more directory entries, which can lead to an infinite loop if not properly handled. To avoid this issue, you should check the return value of readdir and break out of the loop when false is returned.

$dir = "/path/to/directory";

if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        // Process directory entry
    }
    closedir($handle);
}