0

I redirect the visitors in my website from page A to page B. In page B I expect users to get the downloaded PDF file (to be downloaded when page B is loading). I have taken the code from another article (see a previous question answered here) and my code of page B is the following:

<?php
header('Content-Disposition: attachment; filename=nature.pdf');
header('Content-type: application/pdf'); 
$fn=fopen("/wp-content/nature.pdf","r");
fpassthru($fn);
?>

The output is not by opening a download dialog box, instead some unreadable characters are displayed in browser such as the following (I have just picked up a small sample below):

%PDF-1.4 %���� 3 0 obj <>stream x���MK1�o�+�$zIg&�� V=T�=Xo����K��i+#V�yx3��඀(BX�pW`

Server: OS Linux; PHP version: 5.2.17

The visitor -> Browser: Firefox; OS: Windows 2000

Is it possible to fail due to the old OS on client side? If not, does anybody know a solution how to force the download? Any help would be highly appreciated.

Thanks.

Community
  • 1
  • 1
Scrib Dcom
  • 21
  • 1
  • 7
  • try readfile("/wp-content/nature.pdf"); instead of fopen & fpassthru – Venu Jun 24 '12 at 13:43
  • Depending on the size of the download you might want to use `XSendFile` (supported by both lighttpd & apache2 with modxsendfile). Then you would serve the download with: `header('XSendFile: /wp-content/nature.pdf');` and XSendFile would handle the rest. – user268396 Jun 24 '12 at 14:12
  • @Venu - tried already (see below when testing the code suggested by user "take"). Thanks. – Scrib Dcom Jun 24 '12 at 14:43
  • @ user268396: thanks for your suggestion, the file is not big (around 500kb). If applicable, how my code including XSendFile should look like? – Scrib Dcom Jun 24 '12 at 14:57
  • Can you verify that those headers really gets sent? For example by issuing a manual HTTP request using `telnet` or with the Firefox extension LiveHTTPHeaders. – Emil Vikström Jun 24 '12 at 16:35

2 Answers2

1

Try it with the Content-Length header:

ob_clean(); ob_start();
header('Content-Disposition: attachment; filename=nature.pdf');
header('Content-type: application/pdf'); 
header ("Content-Length: ".filesize("/wp-content/nature.pdf"));
readfile("/wp-content/nature.pdf");
exit;
take
  • 2,202
  • 2
  • 19
  • 36
  • Shall I add this line to my code? or replace some existent line? – Scrib Dcom Jun 24 '12 at 13:43
  • But at least now it is user friendly (it does not show anything unreadable in the browser). I think I would add a link for download in addition as the link is valid only a short period of time (then the file is deleted by a cron in backend). – Scrib Dcom Jun 24 '12 at 14:04
0

There was a quirk in the really old browsers when Content-disposition was first being introduced, some of the really old browsers wouldn't show the "Save As" dialogue unless it couldn't recognize the type of file you were trying to open. Try setting the Content-type to nothing (or something unrecognizable), and see if that'll force the older browser to pop the save-as dialogue.

header('Content-type: ');

If that works, then I'd suggest adding in a line of PHP to detect whether or not they're on an old browser before running that line, as modern browsers will use that header to determine what program the file should be opened with.

Sean Johnson
  • 5,567
  • 2
  • 17
  • 22
  • Nope. Not working. My Firefox version is 12.0. Is that too old? I mean I test the code as a client from this Firefox 12.0. – Scrib Dcom Jun 24 '12 at 14:24
  • from your explanation, I assumed that the client that was having the issue was on an older browser. If the browser is still not downloading the file after the HTTP headers have been defined properly, then it may just be a fault in how the client handles it. – Sean Johnson Jun 24 '12 at 14:39
  • No, I said an old OS (operating system), not an old browser. So, if you say "a fault in how the client handles it" what that might be? Is it included due to an old OS? If so, I would perhaps move to test the code from a more modern OS ... Thanks anyway for your input, it is appreciated as any input is trying to solve my problem. I am gratefull. – Scrib Dcom Jun 24 '12 at 14:47