How can one ensure that the HTML files being read and displayed in PHP are secure and do not pose a risk to the website or server?

To ensure that HTML files being read and displayed in PHP are secure and do not pose a risk to the website or server, it is important to properly sanitize and validate the content before outputting it to the browser. This can be done by using functions like htmlspecialchars() to escape special characters and prevent XSS attacks. Additionally, setting strict file permissions and limiting file access can help prevent unauthorized access to sensitive files.

<?php
$file = 'example.html';

if (file_exists($file)) {
    $content = file_get_contents($file);
    echo htmlspecialchars($content);
} else {
    echo 'File not found.';
}
?>