Indsætte funktion i script
Hej.Jeg bruger med fornøjelse dette script, der fungerer som force download, samt tæller antal downloads. Det fungerer fint.
Scriptet:
<?php
### Force download script. Save as downloads.php
### Link like this: http://www.site.dk/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.
$config=array();
$config['email'] = 'min@e-mail.dk';
$config['host'] = 'localhost';
$config['database'] = '*****';
$config['login'] = '*****';
$config['password'] = '*****';
$config['file_path']=$_SERVER['DOCUMENT_ROOT'].'/minMappe';//No Trailing Slash!
$config['allowed_files']=array('doc','txt','pdf','zip','wmv','mpeg','jpg');
$config['PHP_SELF']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
### 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');
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');
}
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);
}
?>
På den side, hvor mine download-filer hentes fra, har jeg indtil videre haft følgende kodestump, som jeg egentlig gerne vil have integreret i ovenstående script, således at alt er samlet i ét script.
<?php include("test/downloads.php");
### Hent en fil ad gangen:
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;
}
?>
Det fungerer sådan set fint nok bare at flytte funktionen "makeDownloadLink()" ind i førstnævnte script, men ville gerne undgå at skulle connecte til databasen igen...hvordan skal funktionen se ud hvis den skal gøre brug af scriptets eksisterende databaseforbindelse?
Håber, det giver mening.
Mvh. :)
