VB icon

Force Download

Email
Submitted on: 1/2/2015 8:23:00 AM
By: brian461 (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: PHP 4.0, PHP 5.0
Views: 773
 
     If you are annoyed with pdf's that open in the browser as I am here is a way to force the user to be prompted to download a file. Changed the $filename directory to the directory that you have all of your pdf, doc, or other binary files. Then just make a call for the file. Example (downdload.php?file=myFile.pdf) and it will prompt the user to open the file for save it to disk.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Force Download
// Description:If you are annoyed with pdf's that open in the browser as I am here is a way to force the user to be prompted to download a file. Changed the $filename directory to the directory that you have all of your pdf, doc, or other binary files. Then just make a call for the file. Example (downdload.php?file=myFile.pdf) and it will prompt the user to open the file for save it to disk.
// By: brian461 (from psc cd)
//**************************************

<?php
if (isset($_GET['file'])) {
 $file = $_GET['file'];
	header("Content-Type: application/force-download\n");
	header("Content-Disposition: attachment; filename=$file");
	$filename = "/usr/local/apache2/htdocs/" . $file;
	$contents = fread(fopen($filename, "rb"), filesize($filename));
	echo $contents;
} else {
	echo "No file specified";
}
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.