Hi.
I have this force download + clicks count script which works fine.
<?php
### Force download script. Save as downloads.php
### Link like this: http://www.site.com/downloads.php?file=doc.pdf
### Create MySQL table query
/*
CREATE TABLE `downloads` (
`ID` tinyint(4) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '0',
`titel` text NOT NULL,
`kategori` varchar(255) NOT NULL default '0',
`downloads` int(4) NOT NULL default '0',
`date` date NOT NULL default '0000-00-00',
PRIMARY KEY (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=20 ;
*/
### config variables.
include_once ("scripts/mysql/sql.php");
$config=array();
$config['email'] = 'my@e-mail.com';
$config['file_path']=$_SERVER['DOCUMENT_ROOT'].'/myFolder'; //No Trailing Slash!
$config['allowed_files']=array('doc','txt','pdf','zip','wmv','mpeg','jpg');
$config['PHP_SELF']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
function download($path,$file)
{
global $config;
header("Expires: 0");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: no-cache, must-revalidate");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Length: '.filesize($path.'/'.$file));
readfile($path.'/'.$file) OR error('Error Reading File');
}
### Get downloadable file by id one at the time
function makeDownloadLink($id){
$db = mysql_connect('localhost', '*****', '*****');
mysql_select_db('*****',$db);
$result = mysql_query("SELECT name, downloads FROM downloads WHERE ID='$id'",$db);
$result = mysql_fetch_row($result);
return $result;
}
function error($text)
{
global $config;
$out = '<p align="center" style="font:12pt Arial"><br><br><br>Fatal Error: <b>'.$text.'</b></p>'."\n";
mail($config['email'],'File Download Error Report',$out,'From: errors@'.str_replace('www.','',$_SERVER['HTTP_HOST'])."\r\n".'Content-Type: text/html'."\r\n");
exit($out);
}
$file=(isset($_GET['file']) && $_GET['file']!='') ? $_GET['file'] : null;
if($file)
{
if(strpos($file,'..')!==false || strpos($file,'/')!==false) error('Access Denied ('.$file.')');
if(!in_array(substr(strrchr($file,'.'),1),$config['allowed_files'])) error('You do not have access to this type of file! ('.$file.')');
if(!is_file($config['file_path'].'/'.$file)) error('File "'.$file.'" is not avaliable!');
$result=mysql_query("SELECT * FROM `downloads` WHERE name='$file'",$conn) OR error('MySQL Query Failed: '.mysql_error());
$query=(mysql_num_rows($result) === 0) ? "INSERT INTO downloads (ID, name, downloads, date) Values ('', '$file', '1', '".date("Y-m-d H:i")."')" : "UPDATE downloads SET downloads = downloads+1 WHERE name='$file'";
mysql_query($query,$conn);
download($config['file_path'],$file);
mysql_close($conn);
}
?>
However, in the function...
### Get downloadable file by id one at the time
function makeDownloadLink($id){
$db = mysql_connect('localhost', '*****', '*****');
mysql_select_db('*****',$db);
$result = mysql_query("SELECT name, downloads FROM downloads WHERE ID='$id'",$db);
$result = mysql_fetch_row($result);
return $result;
}
...I would like to avoid connecting to my db again. How do I do that? Just removing the connection part won't work.
My db config file goes like this:
<?php
$config['host'] = 'localhost';
$config['database'] = '*****';
$config['login'] = '******';
$config['password'] = '******';
### Connect To database
$conn=mysql_connect($config['host'], $config['login'], $config['password']) OR error('MySQL Connect Failed');
mysql_select_db($config['database'],$conn) OR error('MySQL Select DB Failed');
?>
Hope this makes sense.
Thanks in advance.