tags:

views:

31

answers:

2

I let my users download a PDF-file using this code:

The $db->Record['file'] contains the relative path to the document.

The access a dummy page that loads the pdf, so they do not actually see the "text.pdf" in the url. I changed the headers of the file to achieve this:

$downloadfile = '/data/srv/www/vhosts/htdocs'.$db->Record['file'];
$filename = basename($downloadfile);        
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.$filename.'"');        
readfile($downloadfile);

The download works without any problems, but when I try to open it, the following error ocurrs:

The PDF file could not be openend, the type is not supported or the file
was damaged.

How can I fix this?

Not: When I download the file directly, meaning to access is via http://.../text.pdf and open it, everything works fine.

+2  A: 

It is possible that the browser is unable to tell the length of the expected read from the file. From the php documentation, try adding the file size in the header:

header('Content-Length: ' . filesize($file));
Joel Etherton
+1  A: 

(Copied from the comments) I'll bet a beer there are PHP error messages in the file.

Pekka