0
我正在尝试使用PHP脚本获取要下载的文本文件,但是当我的脚本运行时,文件内容显示在浏览器窗口中。我试过这个解决方案:Why downloaded file is not downloaded instead it is shown in browser? 但是,我已经有一个内容长度标题的代码。这是我的PHP代码(这是我从另一个职务1获得和修改文本文件)要下载的php文件在浏览器中显示而不是下载
$file = 'file.txt';
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
Browser output of text file contents
我试图从php.com复制例如,对于PDF文件,我得到的与我链接的帖子中的符号相同。
我不知道为什么会发生这种情况。我在Chrome上运行PHP 5.6.10版的MAMP 3.5。
这让我很困惑,因为到目前为止,我已经能够在网上找到我所做的一切的解决方案。据我所知,我所做的是正确的,所以任何建议将不胜感激。
非常感谢,
Nehal帕特尔
决议:(15年9月4日)的指导马库斯: 删除的flush()的ob_clean()。
$file = 'file.txt';
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
马库斯嗨,我已经试过了,我仍然得到同样的结果。 –
删除'ob_clean();'和'flush();'。只要保留'readfile($ file);' – Marcus
这是可行的Marcus:D非常感谢您的帮助。 –