【发布时间】:2021-04-07 02:08:02
【问题描述】:
想要将页面重定向到文件
function downloadFile($url) {
... // save info to database
$path = ..; // checking if the file is on the local server
if(empty($path) === false) {
// download file from local server - it works
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"". basename($path) ."\"");
readfile($path);
exit;
}
// downloading a file from another server - it does not work, $url is 100% good
header("Location: " . $url);
exit;
}
但是文件没有被扔到屏幕上,我只在“网络”选项卡中看到这个文件
当量:
...file?id=123 // first page run header("Location: " . $url); exit;
并从另一台服务器重定向到页面
...download_file?id=123 // status 302
...file.pdf // status 302
...file.pdf // status 200
最后一页的标题
HTTP/1.1 200 OK
Date: Wed, 30 Dec 2020 15:45:21 GMT
Server: Apache
Last-Modified: Wed, 30 Dec 2020 16:38:38 GMT
ETag: "1a8f32-5b7af7b443bf7"
Accept-Ranges: bytes
Content-Length: 1740594
Content-Disposition: attachment
Content-Type: application/force-download
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
为什么它不在屏幕上显示文件? 为什么它不开始下载? 工具中只有信息(标签网络)
【问题讨论】:
-
如果你直接访问文件的 url 会发生什么?会下载吗?
-
是的,正在下载文件
标签: php nginx downloadfile