Pages: [1]   Go Down
Send this topic | Print
Author Topic: [script] download counter/portal  (Read 784 times)
banancanard
Hyper-Active Member
***
Offline Offline

Posts: 478


Control yourself. Take only what you need from it


WWW
« on: October 01, 2008, 04:38:55 PM »

I was working on a download counter for a friend and I figured others might benefit from it so here you go.
Its very simplistic.  All you have to do is set the mysql details and define the folder where all the downloads are.  Then all you do is go to http://example.com/download.php?f=filename
replacing filename with whatever the file you are downloading is. So for example if you were downloading "game.zip" you would go to
http://example.com/download.php?f=game.zip
This would add one to the counter and then start the download.

To view the stats you go to the view.php file, it is a very very crappy layout, but it gets the job done.

mySql table structure:
Code:
CREATE TABLE IF NOT EXISTS `files` (
  `id` int(8) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `count` int(8) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

download.php:
Code:
<?php
##########################################
#      Author:              Ryan Matthews                              #
#      Program:            Download Counter                   #
#      Created on: September 30, 2008                       #
#      Version:             1.0.0                                           #
#      Description:        Download Counter gets the file name #
#      from the url, checks if it is in the database and then    #
#      incriments or adds it accordingly if it exists.                #
##########################################

# Required information.
$host = ''; // Host of the mysql database.  Usually localhost
$user = '';         // Username associated with the database
$pass = ''; // Password of the username
$data = '';                 // database name
$url = ''; // Main url to the downloadable content
$folder = ''; // folder where all the downloads are do not put a starting or ending / in this variable

# Do not edit below this line
mysql_connect($host, $user, $pass);
mysql_select_db($data);

# Function to test if the file exists using curl
function fileexist($url) {
    
$handle   = curl_init($url);
    if (
false === $handle)
    {
        return
false;
    }
    
curl_setopt($handle, CURLOPT_HEADER, false);
    
curl_setopt($handle, CURLOPT_FAILONERROR, true);
    
curl_setopt($handle, CURLOPT_NOBODY, true);
    
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    
$connectable = curl_exec($handle);
    
curl_close($handle);  
    return
$connectable;
}

# Gets the file name.
$filename = $_GET['f'];

# Gets just the file name without the extention.
$exp = explode('.', $filename);
$file = $exp[0];

# Checks to see if the file is allready in the database.
$result = mysql_query('SELECT * FROM `files` WHERE `name`=\''.$file.'\'');

# Either increments the counter for the file or Checks to see if it exists and adds it to the database.
if(mysql_num_rows($result) != 0){
$result = mysql_query('UPDATE `files` SET `count`=`count`+1 WHERE `name`=\''.$file.'\'');
$check = true;
}else{
# Checks if the file exists
if(fileexist("http://$url/$folder/$filename")){
$result = mysql_query('INSERT INTO `files` (`id`, `name`, `count`) VALUES (\'\', \''.$file.'\', 1)');
$check = true;
}
}

if(
$check == true){
# Uses a header redirect for the download
header("Location: http://$url/$folder/$filename");
}else{
echo
'ERROR!<br />The file you are trying to download doesnt exist.<br />';
}
?>

view.php
Code:
<?php
#####################################
# Author: Ryan Matthews #
# Program: Download Counter Viewer #
# Created on: September 30, 2008 #
# Version: 1.0.0 #
# Description: Download Counter gets the file name #
# from the url, checks if it is in the database and then #
# incriments or adds it accordingly if it exists. #
#####################################

# Required information.
$host = ''; // Host of the mysql database.  Usually localhost
$user = ''; // Username associated with the database
$pass = ''; // Password of the username
$data = '';         // database name

# DO NOT EDIT BELOW
mysql_connect($host, $user, $pass);
mysql_select_db($data);

$result = mysql_query('SELECT * FROM `files`');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View Download Stats</title>
</head>
<body>
<table>
<tr>
<td>| ID |</td><td>| File Name |</td><td>| Downloads |</td>
</tr>
<tr>
<td colspan="3">================================</td>
</tr>
<?php
while(
$row = mysql_fetch_assoc($result)){
echo '
<tr>
<td>| '
.$row['id'].' |</td><td>| '.$row['name'].' |</td><td>| '.$row['count'].' |</td>
</tr>
<tr>
<td colspan="3">================================</td>
</tr>
'
;
}
?>

</table>
</body>
</html>

Logged



Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.
Pages: [1]   Go Up
Send this topic | Print
Jump to: