以下php代码我用来将字符串下载到客户端磁盘。
$str = "Some pseudo-random text spanning multiple lines";
header('Content-Disposition: attachment; filename="sample.txt"');
header('Content-Type: text/plain'); # Don't use application/force-download - it's not a real MIME type, and the Content-Disposition header is sufficient
header('Content-Length: ' . strlen($str));
header('Connection: close');
echo $str;
我从TML here
获取的代码它在我的本地XAMP开发服务器上运行良好。但是,当我将其传输到我的互联网提供商的服务器时,该文件不会存储在磁盘上,而是存在于屏幕上。
也许这是一个关于互联网服务器的错误或缺少php设置的问题,我不知道 任何帮助表示赞赏。
编辑:好的,我遇到了问题。在检查PHP日志错误后,我在设置echo
之前找到了header()
语句。我删除了echo
,现在按预期工作了。
这个问题与下面提到的问题不同。我不想修复标题。我需要遵循正确编写PHP代码的规则。