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.

Did you know?Explore Trending and Topic pages for more stories like this.
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

Help a newbee save my job !
Hey guys i am dying here i am new to php and would like for someone to help me with this problem

Using two $_POST Function / Switch () statements, second does not work.
Hi all. I’m new to php and am having a problem getting $_POST Function / switch () to work. I

Problem with PHP code- simple contact form
Hi Folks,
I'm relativily new to PHP; I know HTML and CSS stuff but I have a problem- I have a co

Give me all your tricks for minimizing jar file size

Hi, I'm coming close to releasing my J2ME game... I am kicking up against the 64k size barrier w

Polymorphism
Was wondering if someone could explain this a little better to me. I had always assumed that it had

form variables from database help.
Hi all,

I would like to have a form that gives you options based on the results of an mysql q

Please help - should be a simple fix.. driving me nuts
Everything seemed to be working fine. I have a table, it alphabetically lists a bunch of cities and

Pagination won't carry results past page 2.
Hi all,

I've worked out my pagination script and its paginating fine until I click next from

passthru() help
Hi everyone,
I'm new to the forum and to PHP. I have some pretty good experience writing shell sc

using explode() to fill in checkboxes
Hi

I have a field stored in a table that contains regions in the UK separated by commas. Ther

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