简体   繁体   中英

nginx php download file after redirect not working

wants to make a redirect to a page to a file

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;
}

but the file is not thrown on the screen, I see this file only in the tab "Network"

eq:

...file?id=123 // first page run header("Location: " . $url); exit;

and redirect to page from another server

...download_file?id=123 // status 302
...file.pdf // status 302
...file.pdf // status 200

header for last page

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

why it doesn't display the file on the screen? why it doesn't start downloading? there is only info in the tool (tab Network)

Isn't not displaying the file on the page because you need change the headers of the page to show it, this probably isn't nothing with nginx but with your PHP (since we don't have too much code to analysis about this situation that you're asking).

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

Anyway, I encourage you to edit your question and try out to give us more explanation and some pice of code example to we analysis it and give you a more concise answer.

solved, as I wrote, the problem is only chrome, the matter of conflict

"@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;

thanks to everyone for your help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM