php 将php脚本输出打印到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16225437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing php script output to file
提问by Chris Hermut
Is there a native function or set of functions in PHP that will allow me to echo
and print php file output into file.
PHP 中是否有一个本机函数或一组函数可以让我将echo
php 文件输出打印到文件中。
For example code will generate HTML DOM that needs to be put into .html file and then displayed as static page.
例如代码将生成需要放入 .html 文件的 HTML DOM,然后显示为静态页面。
回答by Lix
The easiest method would be to create a string of your HTML data and use the file_put_contents()
function.
最简单的方法是创建一个 HTML 数据字符串并使用该file_put_contents()
函数。
$htmlStr = '<div>Foobar</div>';
file_put_contents($fileName, $htmlStr);
To create this string, you'll want to captureall outputted data. For that you'll need to use the ob_start
and ob_end_clean
output control functions:
要创建此字符串,您需要捕获所有输出的数据。为此,您需要使用ob_start
和ob_end_clean
输出控制功能:
// Turn on output buffering
ob_start();
echo "<div>";
echo "Foobar";
echo "</div>";
// Return the contents of the output buffer
$htmlStr = ob_get_contents();
// Clean (erase) the output buffer and turn off output buffering
ob_end_clean();
// Write final string to file
file_put_contents($fileName, $htmlStr);
Reference -
参考 -
回答by Keshav Saharia
file_put_contents($filename, $data)
回答by Rolando Isidoro
PHP provides a few functions that allows you to right to a file, you choose:
PHP 提供了一些函数,允许您正确访问文件,您可以选择: