Download med ID i stedet for navn på fil.
Har dette force-download og kliktæller-script:downloads.php:
----
<?php
### Force download og klik-tæller-script.
### Create MySQL table query
/*
CREATE TABLE `downloads` (
`ID` tinyint(4) NOT NULL auto_increment,
`name` 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;
*/
### Konfigurerbare variabler.
$sql = false;
$conf['host'] = 'localhost';
$conf['base'] = 'xxxxx';
$conf['login'] = 'xxxxx';
$conf['password'] = 'xxxxx';
// Path of the files;
$path = "filer/";
function download($path,$file){
$length = filesize("$path/$file");
header("Content-Type: application/force-download; name=\"$file\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $length");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
readfile("$path/$file");
}
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 downloads WHERE name LIKE '$file'";
$date = date("Y-m-d H:i");
### 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);
if($file){
if(!is_file($path.$file)){
err_msg('Fil ikke tilgængelig!');
}
### The file has not been downloaded yet
if(!$in_db){
$sql = "INSERT INTO downloads (ID, name, downloads, date) Values ('', '$file', '1', '$date')";
### Already downloaded
}
else {
$sql = "UPDATE downloads SET downloads = downloads+1 WHERE name='$file'";
}
}
else{
$res = mysql_query("SELECT ID, name, downloads FROM downloads");
while ($row = mysql_fetch_assoc($res)) {
$ids[] = $row['ID'];
$link_info[$row['ID']]['name'] = $row['name'];
$link_info[$row['ID']]['downloads'] = $row['downloads'];
// echo info[$row['id']]['downloads']." ".info[$row['id']]['name']."<br>";
}
}
if($sql){
$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();
### List all links in DB:
/*echo "<strong>Udskriver alle (".sizeof($ids).") links på række:</strong><br>";
for($i = 0; $i < sizeof($ids); $i++){
// bemærk at det i nogle tilfælde vil være nødvendigt at ændre linkadressen (hardcode link i stedet for $_SERVER['PHP_SELF'])
echo "<a href=\"".$_SERVER['PHP_SELF']."?file=".$link_info[$ids[$i]]['name']."\">".$link_info[$ids[$i]]['name']."</a> - Downloadet ".$link_info[$ids[$i]]['downloads'];
echo $link_info[$ids[$i]]['downloads'] == 1 ? " gang" : " gange";
echo "<br />\n";
}
*/
### get specific link by ID
function getLinkById($ID,$ids,$link_info){
if(in_array($ID,$ids)){
// bemærk at det i nogle tilfælde vil være nødvendigt at ændre linkadressen (hardcode link i stedet for $_SERVER['PHP_SELF'])
echo "<a href=\"".$_SERVER['PHP_SELF']."?file=".$link_info[$ID]['name']."\">".$link_info[$ID]['name']."</a> - Downloadet ".$link_info[$ID]['downloads'];
echo $link_info[$ID]['downloads'] == 1 ? " gang" : " gange";
echo "<br />\n";
}
else
echo "Linket findes desværre ikke - fejlmeld venligst til webmaster!";
}
### Eksempel på brug af ovennævnte funktion:
//echo "<br><strong>Udskriver link efter ID:</strong><br>";
//getLinkById(1,$ids,$link_info);
?>
Jeg kalder linket fx. således: <a href="/downloads.php?file=<?php $myLink = makeDownloadLink(6); ?><?php print($myLink[0]); ?>">Download</a> Det kommer fx. til at se sådan ud på siden: downloads.php?file=navnetpaafilen.pdf - Kan man ikke ændre i scriptet sådan at man kalder filen pr. ID og ikke pr. navn, dvs. fx. sådan: downloads.php?file=12 eller noget i den stil.
Mvh.
