Are there any security risks associated with displaying directory listings in PHP, and how can they be mitigated?

Displaying directory listings in PHP can pose a security risk as it can potentially expose sensitive information about the server's directory structure and files. To mitigate this risk, it is recommended to disable directory listings in the server configuration or use an index file (such as index.php) to control what is displayed to users.

// Disable directory listings in PHP
if (is_dir($path)) {
    header("HTTP/1.1 403 Forbidden");
    die("403 Forbidden");
}