What is the purpose of the opendir() function in PHP?

The opendir() function in PHP is used to open a directory handle, allowing you to read the contents of a directory. This function is useful when you need to iterate through the files in a directory and perform operations on them.

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

if (is_dir($dir)) {
    if ($handle = opendir($dir)) {
        while (false !== ($entry = readdir($handle))) {
            echo $entry . "\n";
        }
        closedir($handle);
    }
}