Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdf download option should appear

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

like image 751
tibin mathew Avatar asked Oct 22 '25 14:10

tibin mathew


1 Answers

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.

like image 198
Tatu Ulmanen Avatar answered Oct 25 '25 03:10

Tatu Ulmanen