Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

Kindly assist me, I'm want to put a pdf file for download on my site, how do i go about it?

The pdf file is already on the server, so how do i make it work?

Kindly respond, thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jul-13 18:12pm    
No need to code a button, it is already "coded". :-)
—SA
namo77 18-Jul-13 18:14pm    
Thanks. But how do I make the file download when the button is clicked?
Dholakiya Ankit 18-Jul-13 23:19pm    
take on anchor tage in href add path of that one it will be downloaded automatically

What's wrong with <a href="myDocument.pdf">Download my document</a>? If you want a button, add a button. Essentially, there is nothing to "code".

See also: http://php.net/manual/en/function.readfile.php[^].

—SA
 
Share this answer
 
v3
Comments
namo77 18-Jul-13 18:49pm    
Thanks, but I've done this, and it doesn't download, it just opens the content of the file. I have to right-click on the button then 'save as' to download. Isn't there a way to download once I click on the button?
Sergey Alexandrovich Kryukov 18-Jul-13 19:01pm    
It depends on your browser. And it's not "just", it downloads and then display. Please see:
http://www.htmlhelp.com/faq/html/links.html#force-download.
—SA
I have answered this question so many times :)

follow the code:

PHP
<?php
	switch( $file_extension )
	{
		case "pdf": $ctype="application/pdf"; break;
		case "exe": $ctype="application/octet-stream"; break;
		case "zip": $ctype="application/zip"; break;
		case "doc": $ctype="application/msword"; break;
		case "xls": $ctype="application/vnd.ms-excel"; break;
		case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
		case "gif": $ctype="image/gif"; break;
		case "png": $ctype="image/png"; break;
		case "jpe": 
		case "jpeg":
		case "jpg": $ctype="image/jpg"; break;
		default: $ctype="application/force-download";
	}
	// echo $base;
	// exit;
	header("Pragma: public"); // required
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false); // required for certain browsers
	header("Content-Type: $ctype");
	header("Content-Disposition: attachment; filename=".$base.".".$file_extension.";" );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".@filesize($rfilename));
	@readfile($rfilename);
	unlink($rfilename);


i only kept the part you need to understand
 
Share this answer
 
Comments
namo77 19-Jul-13 1:48am    
Thanks. This can work. So how do I tie this to a button click event?
Mohibur Rashid 19-Jul-13 1:57am    
onclick='document.location="downloadfile.php?param1=paramvalue1&Param2=paramvalue2"'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900