What are the potential risks of allowing directory listing in PHP applications?

Allowing directory listing in PHP applications can pose a security risk by exposing sensitive information such as file names, directory structures, and potentially confidential data to malicious users. To prevent this, it is recommended to disable directory listing in the web server configuration or within the PHP code itself by setting the "Options -Indexes" directive.

// Disable directory listing in PHP
if (is_dir($directory)) {
    header("HTTP/1.1 403 Forbidden");
    die("Directory listing is not allowed.");
}