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 pulling in 'id' with "read more" link
I'm trying to create snippets of my articles in the cms I'm creating, and then have them redirect to

Variable Clash
In the past I've had variables clash. For example:

Code: <?php
$c = 5;
$ca

Retrieving Data from Two Different Databases
Hello All,

Ran into another problem and would appreciate some help! I have two datab

How to assign JS screen.width to ASP.Net variable
I've got this ASP.net routine that gets the cookie value of the screen width, but now I need to chan

connect() succeeds but accept() does not?
Please help me with this problem. I'm working on a time-sensitive project where I'm using TCP socket

md5 is it unique
I know this is most likely one of many simple questions that can be found semi easily. But I'm press

rdns with php
I am trying to build a script that will show all of the different domains that are hosted on the sam

FTP Programs
Here is a list of commonly suggested FTP Programs to use:

FileZilla
SmartFTP
CuteFTP

Preg_match unknown modifyer
Hello,

Im trying to write a little script for my forums i need to get the reply from my forum

How to make URL loads only on login
On my page, when a user login he's redirects to user_home.php. I made it also to open the chat scree

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