Forum Moderators: coopster

Message Too Old, No Replies

header to download file is corrupt sometimes

Any ideas?

         

stidj

5:08 am on Aug 14, 2006 (gmt 0)

10+ Year Member



I use header to make sure only logged in members can download a certain file but the file download itself is often corrupt and I've tried everything with no solution.

$path = $_SERVER["DOCUMENT_ROOT"]."/members/thefile.pdf";
$download_size = filesize($path);

// force download dialog
header("Content-Type: application/force-download");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
header("Content-Length: ".$download_size);
header('Content-Disposition: attachment; filename=newfile.pdf');
readfile("thefile.pdf");

Even when firefox users download this file it can often be corrupt. This is just one version and combination I've tried but trust me I've tried so many different combinations and I've looked at other forums and even header() discussions in PHP.net

If anyone has encountered this problem and has found a fix it would be appreciated!

Thanks

jatar_k

5:37 pm on Aug 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try looking at the headers used here

Generating a CSV for Download from MySQL [webmasterworld.com] msg 9

and see if that gives any hints.

coopster also posted this update which also might help
Export a table to CSV [webmasterworld.com]

I realize these both deal with csv files but the headers may help

stidj

7:04 pm on Aug 31, 2006 (gmt 0)

10+ Year Member



Thanks I just tried that style of code but changed your "inline" to "attachment" and kept the readfile call otherwise the .pdf won't be sent.

I'll let you know if users report any differences. It seems to be more prevalent on dial-up connections.

My feeling is that for whatever reason the client gets confused and doesn't download all of the data.

I'm not sure if that helps but I've tried to send a header that tells the browser how much data should be coming.

I'll let you know how your code fares and if anyone has the cause and solution it would be appreciated :)

......wait one second. The code you gave me consistently does not work so I've gone back to this which seems to work more often than not but still not 100% :(

header("Cache-Control: ");
header("Pragma: ");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$download_name\"");
header("Content-Length: ".$download_size);
header("Accept-Ranges: bytes");
header("Content-Transfer-Encoding: binary");

Psychopsia

8:32 pm on Aug 31, 2006 (gmt 0)

10+ Year Member



I use this code and always works fine:

header('Content-Type: application/octet-stream; name="file.pdf"');
header('Content-Disposition: attachment; filename="file.pdf"');
header('Accept-Ranges: bytes');
header('Pragma: no-cache');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-transfer-encoding: binary');
header('Content-length: ' . @filesize('real_file.pdf'));
@readfile('real_file.pdf');