1

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)

bad_coder
  • 11,289
  • 20
  • 44
  • 72
dominik
  • 21
  • 3

2 Answers2

0

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.

  • post edited, what else can I add? I did not add any additional headers before the redirect because they seemed unnecessary to me. because this is a redirection and it seemed to me that the header from the last page is the most important (I have no influence on it) – dominik Dec 30 '20 at 15:49
  • i try download this file with readfile but the file is cut on 1.1 GB – dominik Dec 30 '20 at 15:50
  • @dominik What? A pdf file size is greater than 1.1GB??? – adampweb Dec 30 '20 at 15:56
  • is an example of another zip file, pdf max ~100MB – dominik Dec 30 '20 at 16:03
  • Go to your php.ini configuration and check the variable max_allowed_file_size (or something like that), because the php engine's by default is lower than 80mb body requests as far I remember. But, if I'm wrong it's nice / good to go and take a look just in case :) – Fabio William Conceição Dec 30 '20 at 16:23
  • @FabioWilliamConceição current settings: upload_max_filesize 4G and post_max_size 4G – dominik Dec 30 '20 at 16:41
  • @FabioWilliamConceição and in nginx virtualhost configuration client_max_body_size 0; proxy_max_temp_file_size 0; proxy_buffering off; – dominik Dec 30 '20 at 16:48
  • On your PHP file did you set up the headers? – Fabio William Conceição Dec 30 '20 at 16:48
  • @FabioWilliamConceição I add header("Content-type: application/pdf"); header("Content-Disposition: inline; filename=filename.pdf"); header('Content-Transfer-Encoding: binary'); header("Location: " . $url); exit; but the same effect ie does not get – dominik Dec 30 '20 at 17:03
  • a while ago it worked. only for a month or so has stopped. and I do not know if something has broken on the server or something has changed in the headers from that server? – dominik Dec 30 '20 at 17:06
  • What changed for the past 1 month? You can rollback the code in a separated branch and upload it again just for testing? It's seems you nginx really is something goes wrong. – Fabio William Conceição Dec 30 '20 at 19:32
  • the problem only occurs on chrome. on firefox it works fine. I don't have any blockers in chrome – dominik Dec 31 '20 at 08:48
  • 1
    I think I found the answer https://stackoverflow.com/questions/8012279/why-does-chrome-cancel-302-downloads – dominik Dec 31 '20 at 09:06
  • Hi sorry for late response, but yes, this could be work, is that now working? – Fabio William Conceição Jan 05 '21 at 11:17
0

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

dominik
  • 21
  • 3