0

I have headers which force a file download, rather than viewing it in the browser but I need to give that option. I've looked at several posts on Stack Overflow which answer this question (eg How to force files to open in browser instead of download (pdf)?) but I am still not getting the desired result, so I wanted to check if I'm missing something.

Here are my headers to force a download

    $file = 'file.pdf';
    $filePath = 'path/to/file.pdf';

    header("Cache-Control: public");
    header("Content-Length: 20000");
    header("Content-Description: File Transfer");
    header("Content-Type:file");
    header('Content-Disposition: attachment; filename="$file"');
    header("Content-Transfer-Encoding: binary");

    readfile($filePath); 

And here are the header to try and display the file in the browser

    header("Cache-Control: public");
    header("Content-Length: 20000");
    header("Content-Description: File Transfer");
    header("Content-Type:file");
    header('Content-Disposition:inline; filename="$file"');
    header("Content-Transfer-Encoding: binary");

    readfile($filePath); 

So the only change is the content-disposition header.

However, I am still just being shown the markup in the browser (see image).

enter image description here

Any ideas?

Many thanks

Newfoundland
  • 497
  • 6
  • 17

1 Answers1

0

You are still using inline for content-disposition when you want to instruct browser to perform a download. Try use attachment instead.

more detais: Content-Disposition

Yun Luo
  • 1,506
  • 1
  • 9
  • 10
  • Sorry - my mistake, it shoudl be attachment (and is in my code - have corrected the above). In any event, the issue is not with forcing a download it's showing in the browser – Newfoundland Dec 01 '17 at 09:22