gpt4 book ai didi

php - 如何使用 php 将字符串下载到浏览器?不是文本文件

转载 作者:可可西里 更新时间:2023-11-01 00:48:16 25 4
gpt4 key购买 nike

我的问题是我有一个从数据库查询、时间戳和其他随机事物生成的字符串,并且总是不同,所以我没有要下载的"file"。如何使用此生成的字符串在浏览器中为用户下载为 .txt 文件?我知道您可以使用 header 将文件下载到浏览器,所以可能是这样的:

$result = 'Generated string that is different every time. 20121107160322';

header("Content-Type: text/plain");
header("Content-Disposition: attachment; content=$result");
header("Content-Length: " . strlen($result));

显然这行不通,但我什至不知道从哪里开始。

最佳答案

你几乎明白了,你只是缺少将字符串实际输出到浏览器,并且还给它一个浏览器可以用来提示用户的文件名,所以这样做:

$result = 'Generated string that is different every time. 20121107160322';
$filename = 'result_file.txt';

header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Length: " . strlen($result));
echo $result;
exit;

关于php - 如何使用 php 将字符串下载到浏览器?不是文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13279801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com