< ?php
// change this value below
$cs_conn = mysql_connect(‘localhost’, ‘db_user’, ‘password’);
mysql_select_db(‘db_name’, $cs_conn);
mysql_query("CREATE TABLE IF NOT EXISTS `downloaded` (
`filepath` varchar(255) NOT NULL,
`ipadres` varchar(15) NOT NULL,
`last_access` datetime NOT NULL,
UNIQUE KEY `filepath` (`filepath`,`ipadres`),
KEY `ipadres` (`ipadres`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
$path = addslashes($_SERVER[‘REQUEST_URI’]);
$ip = addslashes($_SERVER[‘REMOTE_ADDR’]);
$dl = false;
$sql = sprintf("SELECT UNIX_TIMESTAMP(last_access) last_time FROM downloaded WHERE filepath = ‘%s’ AND ipadres = ‘%s’ ORDER BY last_access DESC", $path, $ip);
$res = mysql_query($sql);
if (mysql_num_rows($res) > 0) {
$last_xs = mysql_result($res, 0, ‘last_time’)+3600;
if ($last_xs < time()) {
mysql_query(sprintf("REPLACE downloaded SET filepath = ‘%s’, ipadres = ‘%s’, last_access = NOW()", $path, $ip));
$dl = true;
}
} else {
$sql = sprintf("REPLACE downloaded SET filepath = ‘%s’, ipadres = ‘%s’, last_access = NOW()", $path, $ip);
mysql_query($sql);
$dl = true;
}
if ($dl) {
$fullPath = $_SERVER[‘DOCUMENT_ROOT’].$path;
if ($fd = fopen ($fullPath, "r")) {
$fname = basename($fullPath);
header(‘Content-type: application/octet-stream’);
header(‘Content-Disposition: filename="’.$fname.‘"’);
header(‘Content-length: ‘.filesize($fullPath));
header(‘Cache-control: private’);
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
fclose ($fd);
exit;
}
} else {
header(‘HTTP/1.0 503 Service Unavailable’);
die(‘Abort, you reached your download limit for this file.’);
}
?>
fonte: www.sastgroup.com





