【发布时间】:2012-02-27 01:27:30
【问题描述】:
我在 webroot 之外保存了许多文档。
我想点击一个链接,打开一个新窗口 (target="_blank"),然后强制下载找到的文件。
这是我到目前为止所得到的,但我的结果在浏览器弹出窗口中显示 gobble-de-gook,而不是强制下载到桌面:
function download($filelocation){
$filename = basename($filelocation);
if (file_exists($filelocation)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filelocation));
ob_clean();
flush();
readfile($filelocation);
exit;
}
}
在新的浏览器窗口中,我只需使用文件的特定路径调用 download() 函数。
它肯定会找到文件,但现在我只是想知道我缺少什么 header() 来强制文件通过浏览器。
【问题讨论】:
-
你试过哪些浏览器?
-
您必须告诉浏览器文件应用程序类型是什么,以便它可以调用适当的处理程序。例如,它是 .jpg 图片还是 .doc 文件? ......就像维克托说的......