How can opendir() be used to list all files in a directory in PHP?

To list all files in a directory in PHP, you can use the opendir() function to open the directory, then loop through the files using readdir() until all files are read. You can then close the directory using closedir(). This allows you to retrieve the list of files in a directory and perform further operations on them.

$dir = "/path/to/directory";
$dh = opendir($dir);

while (($file = readdir($dh)) !== false) {
    if($file != '.' && $file != '..') {
        echo $file . "<br>";
    }
}

closedir($dh);