I'm working on a legacy app running php 5.3 and for some reason on production server PDF files are printed in gibberish straight to the browser instead of being downloaded.
Nothing was changed and in development everything still works fine.
For some reason in prod server, the response is still returned with Content-Type: text/html.
Here is the code:
public function downloadPdf($file) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="FILENAME.pdf"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize("$file"));
ob_clean();
flush();
readfile($file);
exit();
}
I've read numerous tutorials and questions here on Stack Overflow but nothing worked in my case.
I've tried changing:
Content-Type: application/pdf to Content-Type: application/octet-stream
Content-Disposition: attachment; to Content-Disposition: inline;
Checked if file can be read with is_readable() and it is.
Changed readfile($file); to echo file_get_contents($file)
With and without exit()
Tried adding ob_clean() and flush() before setting headers.
Nothing worked for me. Am I missing something? I can't wrap my head around that every thing is working on dev server although everything is identical.
