Reputation: 2084
I need some code to download a pdf file.
what i mean is normaly when we give
like this href="./downloads/Intake Sheet rev 4-1-10.pdf"
that will open the pdf in the same window, i dont want this , i need that to be downloaded , means need the download window to appear.
i'm using php to develop my website.
Please give me some idea.
Thanks
Upvotes: 3
Views: 1158
Reputation: 124768
This behaviour is usually controlled by user itself, but you can build a 'PHP gateway' to force the downloading of the PDF:
<a href="download.php?file=Intake Sheet rev 4-1-10.pdf">Download</a>
And in download.php:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile($_GET['file']);
That should do it but note that this example contains an HUGE security flaw – you MUST check and sanitize the file parameter somehow to prevent users from downloading every file from your server but this should give you the general idea on how to accomplish forced downloads.
Upvotes: 3