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 right.
I want to do like this:
$data=readfile("text.txt");
echo htmlentities($data);
That should give you an idea of how I want that to work.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With