<?
/***************************************************************************
____ _ _ ____ _ _ _ _ _ _
| _ \| | | | _ \| |_ ___ ___ | |___| || | | | | |
| |_) | |_| | |_) | __/ _ \ / _ \| / __| || |_| | | |
| __/| _ | __/| || (_) | (_) | \__ \__ _| |_| |
|_| |_| |_|_| \__\___/ \___/|_|___/ |_| \___/
cpt_download.php - A Download counter
-------------------
begin : Mon Nov 19 2002
updated : Tue March 2 2004
copyright : (C) 2001 PHPtools4U.com - laurent goussard
email : support@phptools4u.com
***************************************************************************/
/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/
/*
- Create the 'files' table with the following query
- Modify the $path variable and set the real path where you store all you files
linux eg. '/data/www/my_files/'
win32 eg. 'C:\www\my_files\\' (note the double \ at the end)
you can also use a relative path...
- Give to cpt_download.php the file you want to log :
http://www.site.com/cpt_download.php?file=the_file_you_want_to_download
- Check your 'files' tables to see the amount of download for each files
### Create table query
CREATE TABLE `files` (
`ID` tinyint(4) NOT NULL auto_increment,
`nom` varchar(255) NOT NULL default '0',
`downloads` tinyint(4) NOT NULL default '0',
`date` datetime NOT NULL,
PRIMARY KEY (`ID`),
KEY `ID_2` (`ID`)
) TYPE=MyISAM;
*/
### Script configuration
// Database authorization
$conf['host'] = 'mysql_host';
$conf['base'] = 'mysql_database';
$conf['login'] = 'mysql_user';
$conf['password'] = 'password';
// Path of the files;
$path = "fichiers/";
function download($chemin,$fichier){
$length = filesize("$chemin/$fichier");
header("Content-Type: application/force-download; name=\"$fichier\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $length");
header("Content-Disposition: attachment; filename=\"$fichier\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
readfile("$chemin/$fichier");
}
function err_msg($text) {
$out = '<center>'."\n";
$out .= '<br><br><br>'."\n";
$out .= '<font face="Arial, Helvetica" color="#D20000" size="2"><b>'.$text.'</b></font>'."\n";
$out .= '</center>';
die($out);
}
$file = ( isset($_GET['file']) && $_GET['file']!='' ) ? $_GET['file'] : null;
$request = "select * FROM files WHERE nom LIKE '$file'";
if(!$file){
err_msg('Spécifiez un fichier <br>('.$_SERVER['PHP_SELF'].'?file=monfichier)');
}elseif(!is_file($path.$file)){
err_msg('Fichier introuvable');
}
$date = date("Y-m-d H:i:s");
### Is the file already in the db ?
$db = mysql_connect($conf['host'], $conf['login'], $conf['password']);
mysql_select_db($conf['base'],$db);
$req = mysql_query($request);
$in_db = @mysql_num_rows($req);
### The file has not been downloaded yet
if(!$in_db){
$sql = "INSERT INTO files (ID, nom, downloads, date) Values ('', '$file', '1', '$date')";
### Already downloaded
}else{
$records = mysql_fetch_array($req);
$rec_download = ($records['downloads']+1);
$sql = "UPDATE files SET downloads='$rec_download', date='$date' WHERE nom='$file'";
}
$db = mysql_connect($conf['host'], $conf['login'], $conf['password']);
mysql_select_db($conf['base'],$db);
$result = mysql_query($sql);
### Send file
download($path,$file);
mysql_close();
?>