Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php生成文档文件;“结果”;页_Php_Html_File_Generated - Fatal编程技术网

从php生成文档文件;“结果”;页

从php生成文档文件;“结果”;页,php,html,file,generated,Php,Html,File,Generated,我有一个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 T

我有一个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手册),即使我不能让它工作


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

这可以帮你:

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

}

这也是一种缓存动态页面的好方法。希望有帮助。

您的代码在my env中运行良好。您是
echo
还是打印ob\u start和ob\u get\u内容之间的值?您所说的“重新创建结果”到底是什么意思;为什么,正如你所说,这是低效的?Cwallenpoole-没有任何回应,有什么是我试图使用的。谢谢,我将通读这篇文章。Bosworth99-我的意思是,我将不得不再次单独写出每个$u post结果-我已经创建了一个表来显示每个结果。使用此方法是可行的,但看起来很笨重。谢谢,但除了创建文件外,它似乎没有向其中写入任何内容?其中显示
//您的内容放在这里
您可以编写任何您想要的内容<代码>回显/打印整页。对不起:这
fwrite($fd,$pages)需要
fwrite($fd,$page)啊哈…这解释了为什么它没有写hello!所以使用这个方法,我需要复制身体的内容,它说你的内容在这里?再次感谢您复制/包括您想要复制的零件周围的脚本。我很高兴能帮上忙。