How can a list of files in a server directory be obtained using PHP?

To obtain a list of files in a server directory using PHP, you can use the opendir() function to open the directory, readdir() function to read the directory contents, and closedir() function to close the directory. You can loop through the directory contents using a while loop and display each file name.

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

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