Page 1 of 1

Help: problem with Headers to download PDF file

Posted: Wed Oct 14, 2009 2:16 pm
by rfeio
Hi,

I'm trying to implement a script where when a user clicks a link he gets a PDF file to open or save.

In my index.php page I have the following link:

Code: Select all

<a href="http://localhost/example/get_file.php">Click here to get the file</a>
And the script is in get_file.php:

Code: Select all

 
<?php
$path = '/docs/document.pdf';
$mm_type="application/pdf"; 
 
 
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " . filesize($path) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
 
readfile($path); 
 
exit();
?>
 
Unfortunately it's not working properly. When clicking on the link and either selecting 'open' or 'save' from the dialog box, the end result is always the same; the end file is about 1KB in size when originally it was 456KB and therefore it's damaged.

Any ideas on why this is happening?

Thanks!

Re: Help: problem with Headers to download PDF file

Posted: Wed Oct 14, 2009 2:48 pm
by rfeio
Ok, I've identified where the problem is.

The path was incomplete. I had to add "http://localhost/..." in order to read the file.

Re: Help: problem with Headers to download PDF file

Posted: Wed Oct 14, 2009 4:14 pm
by requinix
rfeio wrote:Ok, I've identified where the problem is.

The path was incomplete. I had to add "http://localhost/..." in order to read the file.
Nonono.

Use the path as it is on the filesystem, not on the server.

Code: Select all

$path = $_SERVER["DOCUMENT_ROOT"] . '/docs/document.pdf';
Using http://localhost/docs/document.pdf means you're downloading the file from yourself, which is totally pointless because you already have the file.