PHP 在 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() 强制文件通过浏览器时缺少什么。
I save many documents outside the webroot.
I want to click a link, that opens a new window (target="_blank"), and force download the file that's found.
Here's what I've got so far, but my results show gobble-de-gook in the browser popup, rather than forcing the download to the desktop:
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;
}
}
In the new browser window I simply call that download()
function with a specific path the the file.
It's definitely finding the file, but now I'm just wondering what I'm missing with header() to force the file through the browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缺少这个:
Missing this: