0

im trying to download files (pdf or zip). im sending the correct headers

header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=$fileName.pdf");
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
print $pdf;

and i can see the headers in the browser and in headers_list().

response header

i tried multiple things but im not sure why the download never happens, and by that i mean the prompt from the broweser doesnt show (even though i can see the PDF file in the response and i can see its size 45kb sometimes).

and i can see the contents of print in the response as well.

  • 2
    Looks like you're triggering the request via AJAX/XHR. That returns the response to a JavaScript variable within your page, not directly as a response to the browser in a way that could trigger a download. It's up to you to write JS to turn that into downloadable content - there are plenty of examples online if you google "download file via AJAX" or similar. Or, simply don't use AJAX/XHR for this request. – ADyson Jan 18 '22 at 10:00
  • 2
    And `Content-Length: 0`? Probably can't expect the browser to prompt where to store a download, when the amount of data is announced as zero bytes. – CBroe Jan 18 '22 at 10:01
  • Any why are you setting the cache-control header 3 times in the PHP? Only one header with the same name can be sent to the browser (as you can see, the last one you set is sent, since you're basically overwriting the previous value whenever you set it again). – ADyson Jan 18 '22 at 10:27

0 Answers0