【问题标题】:nginx php download file after redirect not working重定向后的nginx php下载文件不起作用
【发布时间】: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


【解决方案1】:

没有在页面上显示文件,因为您需要更改页面的标题才能显示它,这可能与 nginx 无关,但与您的 PHP 无关(因为我们没有太多要分析的代码关于你问的这种情况)。

But if you check this topic here on StackOverflow you'll see a example to show a PDF.

无论如何,我鼓励您编辑您的问题并尝试给我们更多解释和一些代码示例,以便我们分析它并给您一个更简洁的答案。

【讨论】:

  • 帖子已编辑,我还能添加什么?我没有在重定向之前添加任何额外的标题,因为它们对我来说似乎没有必要。因为这是一个重定向,在我看来,最后一页的标题是最重要的(我对此没有影响)
  • 我尝试使用 readfile 下载此文件,但文件被剪切为 1.1 GB
  • @dominik 什么?一个pdf文件大小大于1.1GB???
  • 是另一个 zip 文件的示例,pdf max ~100MB
  • 我想我找到了答案stackoverflow.com/questions/8012279/…
【解决方案2】:

解决了,正如我写的,问题只是chrome,冲突的问题

"@PaoloVeronelli - 当站点通过 HTTPS 加载时,如果通过 HTTP 提供静态内容,Chrome 会阻止加载静态内容。看起来 Firefox 不这样做。——Frederik Nielsen 2015 年 2 月 14 日 15 日: 51"

$url = str_replace('http://', 'https://', $url);
header("Location: " . $url);
exit;

感谢大家的帮助

【讨论】:

    猜你喜欢
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    相关资源
    最近更新 更多