0

我有一个 PHP 生成的页面,其中包含提交表单的结果,我想做的是将其保存为服务器上的 .doc 文件。经过一番谷歌搜索后,我发现了我改编的这段代码:-

$myFile = "./dump/".$companyName."/testFile.doc";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);

但是这样做的问题是我必须重新创建结果才能手动将它们写入文件,而且它似乎效率不高。

所以我继续谷歌并找到了让我坦率地挠头的PHP手册,但我最终发现了这个: -

ob_start();
// code to generate page.
$out = ob_get_contents();
ob_end_clean();
// or write it to a file.
file_put_contents("./dump/".$companyName."/testFile.doc",$out);

这将创建文件,但不会向其中写入任何内容。然而,这似乎是我想做的事情(基于 PHP 手册),即使我无法让它工作!

有什么建议吗?如果我能找到一个像样的搜索词,我不介意谷歌搜索:)

4

1 回答 1

1

这很适合你:

$cache = 'path/to/your/file';

ob_start();

// your content goes here...
echo "hello !"; // would put hello into your file

$page = ob_get_contents(); 

ob_end_clean(); 

$fd = fopen("$cache", "w"); 

    if ($fd) {

    fwrite($fd,$page); 

    fclose($fd);

}

这也是缓存动态页面的好方法。希望能帮助到你。

于 2011-07-27T14:59:42.627 回答