PHP创建文件供下载而不保存在服务器上

时间:2023-02-06 08:20:44

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.

终极目标:我想创建一个用户可以在表单中输入信息的网页。有了这些信息,我想通过将给定的信息插入模板然后强制下载来创建一个html文件(下面称为test-download.html)。由于我想在即将举行的研讨会上展示这一点,人们将在“同一时间”使用它,我不想将文件保存在服务器上,只是强制下载。

So far: I have this in my html file (test.html):

到目前为止:我在我的html文件(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:

这在我的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.

这将覆盖文件test-download.html文件并强制下载。

Question: How can I do this without messing with a file (the test-download.html) on the server?

问题:如何在不弄乱服务器上的文件(test-download.html)的情况下执行此操作?

2 个解决方案

#1


34  

Instead of saving it to a file, just echo it after you send the headers.

而不是将其保存到文件,只需在发送标头后回显它。

#2


19  

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".

意识到几乎每次PHP脚本响应请求时,它都会“生成一个由浏览器下载的文件”。您回显,打印,打印或以其他方式输出到标准输出的任何内容都是该“文件”的内容。

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.

您所要做的就是告诉浏览器应该以不同的方式处理“文件” - 您输出的标题应该已经这样做了。发送标题后,您打印的任何内容都将成为下载内容。

#1


34  

Instead of saving it to a file, just echo it after you send the headers.

而不是将其保存到文件,只需在发送标头后回显它。

#2


19  

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".

意识到几乎每次PHP脚本响应请求时,它都会“生成一个由浏览器下载的文件”。您回显,打印,打印或以其他方式输出到标准输出的任何内容都是该“文件”的内容。

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.

您所要做的就是告诉浏览器应该以不同的方式处理“文件” - 您输出的标题应该已经这样做了。发送标题后,您打印的任何内容都将成为下载内容。