You can do this many ways, but they don't all work in all browsers/platforms and many of the options out there don't include all the features.
So... here is the best I've found so far
Perl solution -
You would save the code below in a file with the .pl extension or cgi (I used download.cgi below), upload it to your server (FTP) and set the permissions to 755 (you may need to call your host godaddy to help you with these things).
Here is the link you would use for your media -
<a href="http://www.domain.com/download.cgi?ID=file.name">Download File</a>
Code:
#!/usr/bin/perl -wT
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
my $files_location;
my $ID;
my @fileholder;
$files_location = "/www/g/gcm345/htdocs/MP3/";
$ID = param('ID');
# require file name
if ($ID eq '') {
print "Content-type: text/html\n\n";
print "You must specify a file to download.";
} else {
# If a file name is specified, we want to open that file and
# assign its contents to a temporary placeholder, and display an error message.
open(DLFILE, "<$files_location/$ID") || Error('open', 'file');
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');
# Log the number of times each file is downloaded, or an error message.
open (LOG, ">>/www/g/gcm345/htdocs/MP3/dl.log") || Error('open', 'file');
print LOG "$ID\n";
close (LOG);
print "Content-Type: application/x-download\n";
# print "Content-Length: {'Content-length'}\n";
print "Content-Disposition: attachment;filename=$ID\n\n";
print @fileholder
}
# prrints browser error message
sub Error {
print "Content-type: text/html\n\n";
print "The server can't $_[0] the $_[1]: $! \n";
exit;
}
Now this offering works, but it doesn't address client side caching for saving of bandwidth. You also have to create a log file (empty text file) name it dl.log and upload to the dir where your media is. Then change the permissions to 777 on that file.
Also - you can create an .htaccess file (do a search on it for lots of how to) place this file at the root of your hosting account, maybe even one level higher. With this in it -
Code:
Options -indexes
AddType application/octet-stream .mp3 .mpeg
<Files .htaccess>
order allow,deny
deny from all
</Files>
Where .mp3 .mpeg need to be changed to the extension of the type of media you want to download. The other entries are just for security and you can do searches on them for more info - as this post is growing way to long!
And the last option I would submit (but I haven't tried it) is using php like this -
Code:
<?php
$filename = $_GET['file'];
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
exit;
};
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 "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
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");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>
You would have to save this in file with .php file extension, upload to site, and use this for your link - <a href="
http://www.yoursite.com/force-download.php?file=filepath">
I wish there was a simple, secure and easy solution, but all of those turn out not to work in all browsers or in some other way....
I hope this helps
