Got the vacations and so it's coding time. Pretty basic php based download script with a counter before starting the download, you may have seen those kind of script sources all around. But I made some arrangements for this code to make it a bit more than a simple downloading script. Lets start then.

Background

What is a Download Script?

The best way to explain is the sites like sourceforge.net and cnet.com where when you select a file to download, it wont give you a direct link, but directs you to a page where a download will start after soem seconds or immediately or even you can select another mirror and stuff.

Why to use a Download Script?

It makes your site/application more professional.

You can easily implement things like download counter to the page.

Can reduce a bit of certain automated scripts from downloading your script

How it Works?

Those scripts change the header content type through the script so the browser identifies anything comming from that script as something downloadable.

download.php -> handle the things that the user sees

downloader.php -> prepares and send the file to the user without showing to the user

Logic: Most of the time, download.php will be using an iframe to load the downloader.php and hence the user see no difference

The Code

First we need to make a table with the links to the files. Use the following query for the table. You should add ALL the links that you allow the use to download. The download script is called from the "ID" of the selected product.

//First getting the id specified as download.php?id=1
$id = htmlspecialchars($_GET['id']);
//This variable is a bool to check for any error, as we should have tight security
$error = false;
//Connecting to the database
$conn = mysql_connect(db_host,db_user,db_pwd);
if(!($conn)) echo "Failed To Connect To The Database!";
else{
if(mysql_select_db(db_name,$conn)){
try{
$qry = "SELECT Name,Link FROM downloads WHERE ID=$id";
$result = mysql_query($qry);
//Check whether there's a result with the id number
if(mysql_num_rows($result)==1){
while($rows = mysql_fetch_array($result)){
//If there's one take the details into variables
$nam = $rows['Name'];
$link = $rows['Link'];
}
//Updating the counter by 1
$qry = "UPDATE downloads SET Counter = Counter+1";
mysql_query($qry);
}else{ $error = true; }
}catch(Exception $e){
//If an Exception occurrs make $error true
$error = true;
}
// At last check whether $error is true, if so immediatly redirect to the home page so that an attacker cannot take use of any error
if($error) header("Location: index.php");

}
}
?>
<html>
<head>
<title>Download Script</title>
<script type="text/javascript">
window.onload = function(){
//Creates a new iframe
var ele = document.createElement('iframe');
//Navigate the iframe to the downloader.php which in turn return the file
ele.src = "downloader.php?id=<?php echo $id; ?>";
//Hide the stuff from the user
ele.style.display = "none";
//Add the iframe to the document body
document.body.appendChild(ele);
}
</script>
</head>
<body>
Thank You For Downloading <?php echo $nam; ?> <!-- note the use of the Name field to show the user what he is downloading -->
</body>
</html>

Now we make the downloader.php which will recieve the same ID as the download.php and then it will return a file back.

//Code above this part was more or less the same as the previous, but the code below plays an IMPORTANT role

//First we need a name to the file we are sending to the user, by defualt it will be saying something which include the complete path + file name and some blahh. But it is better to have the same sweet name you put for the original fiel to be here as well :D
//pathinfo returns an array of information
$path = pathinfo($f);
//basename say the filename+extension
$n = $path['basename'];
//NOW comes the action, this statement would say that WHATEVER output given by the script is given in form of an octet-stream, or else to make it easy an application or downloadable
header('Content-type: application/octet-stream');
//This would be the one to rename the file
header('Content-Disposition: attachment; filename='.$n.'');
//Finally it reads the file and prepare the output
readfile($f);
//If you goto to the same page with your browser a download will start immediately
}else $error = true;
}catch(Exception $e){
$error = true;
}
if($error) header("Location: index.php");
}
}
?>

Now everything's fine, when you goto http://mysite.com/download.php?id=1 and if you have the file prod1.zip in files directory (files/prod1.zip) your download will start immediately or giveout a status bar saying a file is going to download.

Lets put up a bit of a code so that a counter of 5 second go before the thing starts so that if the user doesnot wanna download the file he can quit before it. For that edit the html part of download.php as follow:

So that's it, you can implement this easily to your site and such simple method will make you site look more user friendly and professional to the users and even your management of files become easier.