How can opendir and readdir functions be utilized to read and display directory contents in PHP?

To read and display directory contents in PHP, you can use the opendir() function to open a directory handle and then use the readdir() function to read the contents of the directory one by one. You can loop through the directory contents and display them as needed.

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

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != "." && $file != "..") {
                echo "filename: $file : filetype: " . filetype($dir . $file) . "<br>";
            }
        }
        closedir($dh);
    }
}