0

I've used PHP to mask the directory for PDF downloads for years with the following code with no issues. Then I upgraded from PHP 5.6 to 7.2 yesterday and the code no longer works, but I'm stuck figuring out what the problem is.

Currently, if I click on the download link, the file downloads, but then, at the very end, there is an error message reading "Failed-Network Error" (in Chrome) and "The Network connection is interrupted" (in Safari) and the file can't be opened.

The error log on the server is empty.

Code snippet follows:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=".$filename.";");
header("Content-Length: ".filesize($filepath.$filename)."");
ob_end_flush();
readfile($filepath.$filename);

Now if I change the following:

Content-Disposition: attachment;

to:

Content-Disposition: inline;

Then the above works, but as some of the PDFs are large (they range in size from 2 MB to about 80MB), this isn't really a desirable situation and I'd like the file being downloaded to be the default action.

Any thoughts much appreciated.

Thanks!

  • Could it be as simple as missing a semicolon after $filename `header("Content-Disposition: attachment; filename=$filename;");` If not, what is the value of $filename? – Tristan Dec 09 '18 at 22:51
  • Thanks. Tried adding in the semi-colon, no change. Filenames vary, for eg "2016-cherating.pdf" what is weird is they display correctly when I use "inline" so the problem seems to be rooted in the "attachment" bit. – Stuart at Travelfish Dec 09 '18 at 22:57
  • Also check capitalisation of `Content-Type: application/pdf` – Tristan Dec 09 '18 at 23:26
  • I would personally suggest not using the `@` operator since that can cause important errors/warnings to be silenced that could help in debugging. [This answer](https://stackoverflow.com/a/7263943/1344955) might help. – SeinopSys Dec 10 '18 at 00:14
  • Have corrected the capitalisation and removed @ operator, still no dice. Thanks all for the suggestions so far. – Stuart at Travelfish Dec 10 '18 at 01:01
  • 1
    Turns out what was missing was `exit;` At the end. I guess the older PHP tolerated that, but 7.2 spat the dummy. Now working fine. Thanks for the suggestions all. – Stuart at Travelfish Dec 10 '18 at 05:46

0 Answers0