将浏览器中显示的内容写入文件php?

write contents that display in browser to a file php?

我想将在浏览器中显示的内容写入一个文本文件。我想在浏览器中显示数据并写入文本文件。这是我的脚本。我使用 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);
?>

我会这样编码

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

?>