What potential issues could arise when using the readdir() function in PHP?

One potential issue when using the readdir() function in PHP is that it may return false when there are no more directory entries to read, which can lead to an infinite loop if not handled properly. To solve this issue, you can use a while loop to iterate over the directory entries until readdir() returns false.

$dir = '/path/to/directory';
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            // Process each file or directory entry here
        }
        closedir($dh);
    }
}