What potential security risks are associated with directly displaying file paths in the browser window?

Displaying file paths directly in the browser window can expose sensitive information about the server's directory structure, potentially aiding attackers in identifying vulnerabilities or conducting targeted attacks. To mitigate this risk, it is recommended to sanitize and validate file paths before displaying them to users. This can be achieved by using PHP's realpath() function to resolve the path to its absolute form and ensure it is within the expected directory.

<?php
// Example file path
$file_path = '/var/www/html/uploads/file.txt';

// Sanitize and validate file path
$absolute_path = realpath($file_path);

if ($absolute_path && strpos($absolute_path, '/var/www/html/uploads/') === 0) {
    echo "Absolute path: " . $absolute_path;
} else {
    echo "Invalid file path";
}
?>