PHP 创建文件供下载而不保存在服务器上
最终目标:我想创建一个网页,用户可以在其中输入表单信息。有了这些信息,我想通过将给定的信息插入到模板中来创建一个 html 文件(下面称为 test-download.html),然后强制下载。因为我想在即将举行的研讨会上演示这一点,人们将“同时”使用它,所以我不想将文件保存在服务器上,而只是强制下载。
到目前为止: 我的 html 文件 (test.html) 中有这个:
<form action="test.php" method="post">
To file: <input type="text" name="tofile" />
<input type="submit" />
</form>
我的 test.php 中有这个:
<?php
$filename = 'test-download.html';
$htmlcode1 = "<HTML> \n <BODY>";
$htmlcode2 = "</BODY> \n <HTML>";
$somecontent = $htmlcode1.$_POST["tofile"].$htmlcode2;
!$handle = fopen($filename, 'w');
fwrite($handle, $somecontent);
fclose($handle);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
?>
这会覆盖文件 test-download.html 文件并强制下载。
问题:如何才能做到这一点而不弄乱服务器上的文件(test-download.html)?
Ultimate goal: I want to create a webpage where a user can enter information in forms. With that information I want to create a html file (below called test-download.html) by inserting the information given into a template and then force a download. Since I want to demonstrate this at an upcoming workshop where people will be using this at the "same time" I would like to not save the file on the server and just force the download.
So far: I have this in my html file (test.html):
<form action="test.php" method="post">
To file: <input type="text" name="tofile" />
<input type="submit" />
</form>
and this in my test.php:
<?php
$filename = 'test-download.html';
$htmlcode1 = "<HTML> \n <BODY>";
$htmlcode2 = "</BODY> \n <HTML>";
$somecontent = $htmlcode1.$_POST["tofile"].$htmlcode2;
!$handle = fopen($filename, 'w');
fwrite($handle, $somecontent);
fclose($handle);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
?>
This overwrites the file test-download.html file and forces a download.
Question: How can I do this without messing with a file (the test-download.html) on the server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需将其保存到文件中,只需在发送标头后
echo
即可。Instead of saving it to a file, just
echo
it after you send the headers.意识到几乎每次 PHP 脚本响应请求时,它都会“生成一个文件”并由浏览器下载。您
echo
、print
、printf
或以其他方式输出到标准输出的任何内容都是该“文件”的内容。您所要做的就是告诉浏览器应该以不同的方式处理“文件”——并且您输出的标头应该已经这样做了。发送标头后,您打印出来的任何内容都将成为下载内容。
Realize that nearly every time a PHP script responds to a request, it's "generating a file" that's downloaded by the browser. Anything you
echo
,print
,printf
, or otherwise put out to standard output is the contents of that "file".All you have to do is tell the browser that the "file" should be handled differently -- and the headers you're outputting should already do that. Once the headers are sent, anything you print out becomes contents of the download.