Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I assign content from readfile() into a variable?

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.

like image 837
netrox Avatar asked Jun 07 '12 19:06

netrox


Video Answer


1 Answers

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.

like image 185
Paul Avatar answered Nov 09 '22 17:11

Paul