What potential issues or errors can arise when using opendir to read directory contents in PHP?

One potential issue when using opendir to read directory contents in PHP is forgetting to close the directory handle after reading the contents. This can lead to resource leaks and potential performance issues. To solve this problem, make sure to always close the directory handle using closedir() after reading the contents.

$dir = opendir('/path/to/directory');

while (($file = readdir($dir)) !== false) {
    // process the file
}

closedir($dir);