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

ε祈祈猫儿з 提交于 2020-01-21 08:03:21

问题


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.


回答1:


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.




回答2:


readfile() writes the contents of the file to a buffer. Use file_get_contents() instead.



来源:https://stackoverflow.com/questions/10938822/how-do-i-assign-content-from-readfile-into-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!