How can fread() and nl2br() functions be used instead of include() in PHP for better file handling?

When including files in PHP, it's important to handle the file content properly to avoid potential security risks. Instead of using the include() function, which directly executes PHP code, a safer approach is to use fread() to read the file content and nl2br() to convert newlines to HTML line breaks. This ensures that the file content is processed as plain text and any potential PHP code within the file is not executed.

$file = fopen('file.txt', 'r');
$content = fread($file, filesize('file.txt'));
fclose($file);

echo nl2br($content);