如何将readfile()中的内容分配给变量? [英] how do I assign content from readfile() into a variable?

查看:122
本文介绍了如何将readfile()中的内容分配给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

readfile()表示它输出内容,但我希望将内容保存在变量中.我怎么做?我试过$ content = readfile("file.txt"),但结果不正确.

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.

我想这样做:

$data=readfile("text.txt");
echo htmlentities($data);

这应该使您了解我希望它如何工作.

That should give you an idea of how I want that to work.

推荐答案

这就是 file_get_contents 的用途:

That is what file_get_contents is for:

$data = file_get_contents("text.txt");
echo htmlentities($data);

如果必须使用 readfile ,则可以使用输出缓冲将内容转换为变量:

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);

在这种情况下,我不明白为什么您会避免使用 file_get_contents .

I don't see why you would avoid file_get_contents in this situation though.

这篇关于如何将readfile()中的内容分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆