【问题标题】:write contents that display in browser to a file php?将浏览器中显示的内容写入文件 php?
【发布时间】:2015-05-06 06:21:44
【问题描述】:

我想将浏览器中显示的内容写入文本文件。我想在浏览器中显示数据并写入文本文件。这是我的脚本。我使用 file_put_contents 写入文本文件。但是当我访问我的文本文件时,我看不到任何数据。

<?php
$dfile = fopen("/usr/share/dict/words", "r");
while(!feof($dfile)) {
$mynextline = fgets($dfile);
if (preg_match("/lly/", $mynextline)) echo "$mynextline<br>";
}
$file = 'mycontent.txt'
file_put_contents($file, $mynextline);
?>

【问题讨论】:

  • 您显示的那段代码显然不完整。没有file_put_contents()。请添加这些详细信息。
  • 我很抱歉!我在这里发布之前已经编辑了文件。
  • 你看过这个吗? php.net/manual/en/book.outcontrol.php 或者,您可以将输出放在字符串中,然后将其输出到浏览器和文本文件。

标签: php file text


【解决方案1】:

我会写成这样的代码

<?php
    $dfile = fopen("/usr/share/dict/words", "r");
    $ob_file = fopen('mycontent.txt',"w");
    while(!feof($dfile)) {
        $mynextline = fgets($dfile);
        if (preg_match("/lly/", $mynextline)) {
            echo "$mynextline<br>";
            fwrite($ob_file, $mynextline);
        }
}

fclose($dfile);
fclose($ob_file);

?>

【讨论】:

  • 现在我明白了。我需要打开最初应该声明的两个文件,然后将输出写入文本文件。太棒了,它奏效了!!
  • 问题在于,在最后 2 行中,您只输入了您阅读的最后一个字符串。你需要把它写在while循环中;)
  • 我理解@SpongePablo。非常感谢你。写剧本的时候我会做笔记的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多