如何在PHP中使用Fopen和Fwirte创建Word文档后提示另存为对话框
我编写了以下脚本,以便最终用户可以根据输入文本区域的文本创建 Word 文档:
$fp = fopen("yourDoc.doc", 'w+');
fwrite($fp, $wordDoc);
fclose($fp);
基本上,正在创建的文件是“yourDoc.doc”,写入该文件的文本位于 $wordDoc 变量中。
目前,此脚本创建文档并自动将其保存到我的服务器页面所在的同一路径。我想要做的是使用上面的脚本,但提示“另存为”对话框,以便最终用户可以将文档保存到本地计算机。
经过几个小时的研究,我发现我可以使用标头,但是我无法使它们工作。我似乎找不到包含 headers、fopen 和 fwrite 的示例。例如,我尝试了以下脚本的许多变体,但没有成功:
$fp = fopen("yourDoc.doc", 'w+');
fwrite($fp, $wordDoc);
fclose($fp);
header('Content-type: application/ms-word');
header('Content-disposition: attachment; filename="yourDoc.doc"');
readfile("yourDoc.doc");
我还尝试了以下内容(这是我的整个 php 页面):
<?php
//Connect to Db//
$con = mysql_connect("localhost", "root", "admin");
//Verify connection//
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Select Db//
mysql_select_db("schooldb", $con);
$wordDoc = $_GET['word'];//text for word doc//
header('Content-type: application/ms-word');
header('Content-disposition: attachement; filename="yourDoc.doc"');
$fp = fopen("yourDocument.doc", 'w+');//word doc created//
fwrite($fp, $wordDoc);//text written to word doc//
fpassthru($fp);
fclose($fp);
mysql_close($con);
?>
有人看到这个有问题吗?该文件最终出现在我的服务器上,并且我没有看到“另存为”对话框。
我真的需要这方面的帮助。
谢谢,
I wrote the following script so that an end-user can create a word document from text entered into a textarea:
$fp = fopen("yourDoc.doc", 'w+');
fwrite($fp, $wordDoc);
fclose($fp);
Basically, the file being created is "yourDoc.doc", and the text written to the file is located in the $wordDoc variable.
Currently, this script creates the document and automatically saves it to the same path where my server pages are located. What I want to do is use the above script, but prompt a "Save As" dialog box so that the end-user can save the document to their computer locally.
After a few hours of research, I saw that I could use headers, however I am unable to make them work. I cannot seem to find an example that includes headers, fopen, and fwrite altogether. For example, I tried many variations of the following script with no luck:
$fp = fopen("yourDoc.doc", 'w+');
fwrite($fp, $wordDoc);
fclose($fp);
header('Content-type: application/ms-word');
header('Content-disposition: attachment; filename="yourDoc.doc"');
readfile("yourDoc.doc");
I also tried the following (this is my entire php page):
<?php
//Connect to Db//
$con = mysql_connect("localhost", "root", "admin");
//Verify connection//
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Select Db//
mysql_select_db("schooldb", $con);
$wordDoc = $_GET['word'];//text for word doc//
header('Content-type: application/ms-word');
header('Content-disposition: attachement; filename="yourDoc.doc"');
$fp = fopen("yourDocument.doc", 'w+');//word doc created//
fwrite($fp, $wordDoc);//text written to word doc//
fpassthru($fp);
fclose($fp);
mysql_close($con);
?>
Does anyone see something wrong with this? The file ends up on my server, and I do not get a Save As dialog box.
I really need help on this.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需先将字数据写入文件:
但是,您必须确保在执行 header() 调用之前没有发生任何输出。即使出现一个“输出”字符,PHP 也不会发送标头,并且这将失败。
There's no need to write the word data out to a file first:
however, you have to ensure that no output whatsoever has occured before you do the header() calls. PHP will NOT send headers if even a single character of "output" has occured, and this will fail.