F.P
F.P

Reputation: 17831

Downloading PDF - can't open it

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.

Upvotes: 1

Views: 2054

Answers (3)

Kevin Van Dyck
Kevin Van Dyck

Reputation: 105

Did you miss a / after htdocs in the first line?

Upvotes: 0

Pekka
Pekka

Reputation: 449415

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

Upvotes: 2

Joel Etherton
Joel Etherton

Reputation: 37533

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));

Upvotes: 3

Related Questions