There are times when you want to host some files but want people to download them instead of getting them opened by the default browser defined action. This situation arises in cases when you are hosting pdf, image or music files. If you are using Wordpress, you may use Download manager plugins for this effect but what happens when you are on a static website. There are .htaccess file hacks or apache tricks to achieve this. But not all webhosting companies allow such mods or hacks. The one hack I am going to tell you will work no matter which webhost you are using and will work on both blogs and static websites as well.
Step 1
First make a empty file and name it as download.php. Paste the following code into it:
<?php
$sourceFile = $_GET['file'];
$outputFile = $sourceFile; // the name they save as can be different to the existing file
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
switch( $fileExtension)
{
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 "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
session_cache_limiter("");
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=".basename($outputFile).";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($sourceFile));
@readfile("$sourceFile") ; // This is the bit that prompts for the download, supress errors for niceness
exit();
?>
Step 2
Now upload the file to your website … for example I am considering the location as http://www.domain.com/download.php
Step 3
Now for linking to the file you want people to download forcibly, just point their links as http://www.domain.com/download.php?file=Filepath
Now, all files you link to like this will be forced to download from the visitor irrespective of the browser the user uses and of the webhosting you use.








works great, except for in google chrome.
Chrome automatically downloads what seems to be the file header (the file exists on the hard drive but has a size of 0 bytes). any clues?
That is a great addition. I’m always frustrated when i’m trying to get a user to download a text file that I have posted and they keep coming back to me saying they can’t download it and that it just opens in a new browser window.
Thanks for the tip.
That’s a great trick. Nice one. Will try it once now . lets see.
A nifty trick. Saves meddling with the .htaccess file, which, because the latter is essential to Wordpress’ operation, is A Good Thing.