Help: problem with Headers to download PDF file


Posted on 16th Feb 2014 07:03 pm by admin

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

Code: <?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!

No comments posted yet

Your Answer:

Login to answer
243 Like 28 Dislike
Previous forums Next forums
Other forums

PHP, Arrays & A Lot Of Confusion
I've been doing basic PHP work on sites for a few years now, but I've never been tempted to learn ho

IS Retail & Manufacturing
Hi

Can IS Retail and Manufacturing activities be done in same instance ie. in same client

form submit at godaddy?
Trying to test form submission at GoDaddy and it does not seem to work. Here's the page...

h

reating a background image
I am building a site in drupal and have a php form in it, due to certain annoyances with module buil

Coding question?
Hey guys, I have a quick question. If I want to make a way for people to pay for health in my game,

MySQL Primary key gap
Alright, so I have a table with 26 entries in it (id, filename, caption) for my image randomiser (ht

Auto-populating dropdowns and multiple forms.
Here's what I have so far:

First drop down = select a state (works)
This populates the sec

Storing Values taken from a DB...
Using this code it will generate a short list from my database

<?php
mysql_connect

php/mysql auto logout after 2 hour and reset password
Hi all,

This I hope will make sense. I've the following code which when a user logins in, cr

$GPRMC and NMEA how to extract from report
Hi There,

Im a little bi lost and not sure where to start with this one, ive got a small gps

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash