Hello, I'm trying to implement direct download when a user clicks on a link. Here's the code I have now.
Javascript.
function downloadFile(id) {
sendwin = window.open("./download.php?id=" + id,'downloadWindow', 'width=400,height=100,scrollbars=no,menubar=no');
}
Link
<a href="./myfile.mp3" onClick="downloadFile('<?=$row['id'];?>'); return false;">Download myfile.mp3</a>
Download Page
function protect($var) {
$var = mysql_real_escape_string(stripslashes(striptags(trim($var))));
return $var;
}
$id = protect($_GET['id']);
if($id) {
$sql = "SELECT * FROM Files WHERE ID = '$id'";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
$file = basename($row['fileName']); // strip any directory data
$dir = '/public_html/Files/';
if(is_readable($dir.$file))
{
header('Content-Length: '.filesize($dir.$file));
header("Content-Disposition: attachment; filename=$file");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($dir.$file);
exit;
echo "The File You Requested Will Start Downloading Shortly.\n";
}else {
die("There Was An Error Opening The File.");
}
}
}else {
die("Please Select A File to Download");
}
Thanks in advance.