readfile() says it outputs the content but I want the content to be saved in a variable. How do I do that? I tried $content=readfile(\"file.txt\") but that doesn\'t come out
That is what file_get_contents is for:
$data = file_get_contents("text.txt");
echo htmlentities($data);
If you must use readfile you can use output buffering to get the contents into a variable:
ob_start();
readfile("text.txt");
$data = ob_get_clean();
echo htmlentities($data);
I don't see why you would avoid file_get_contents in this situation though.
readfile() writes the contents of the file to a buffer. Use file_get_contents() instead.