簡體   English   中英

nginx php 重定向后下載文件不起作用

[英]nginx php download file after redirect not working

想要將頁面重定向到文件

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

header 最后一頁

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

為什么它不在屏幕上顯示文件? 為什么它不開始下載? 工具中只有信息(選項卡網絡)

沒有在頁面上顯示文件,因為您需要更改頁面的標題以顯示它,這可能與 nginx 無關,但與您的 PHP 無關(因為我們沒有太多代碼來分析這種情況你問的)。

但是,如果您在 StackOverflow 上查看此主題,您將看到一個顯示 PDF 的示例。

無論如何,我鼓勵你編輯你的問題並嘗試給我們更多的解釋和一些代碼示例,以便我們分析它並給你一個更簡潔的答案。

解決了,正如我寫的,問題只是chrome,沖突的問題

"@PaoloVeronelli - Chrome blocks the static content from loading if it is being served over HTTP when the site is loaded over HTTPS. It could look like Firefox doesn't do this. – Frederik Nielsen Feb 14 '15 at 15:51"

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

感謝大家的幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM