Hi.
I'm using this force download and click counter script for my downloadable pdf documents. It works fine from my download page but when clicking a download link from i.e. a Google search, I get error messages saying that the document could not be opened. This only happenes in IE and not in browsers lile Mozilla Firefox.
Thus is the script:
<?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;
*/
### config variables.
$sql = false;
$conf['host'] = 'localhost';
$conf['base'] = '*****';
$conf['login'] = '*****';
$conf['password'] = '*****';
// Path of the files;
$path = "myfiles/";
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('File not avaliable!');
}
### 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();
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;
}
### get specific link by ID
function getLinkById($ID,$ids,$link_info){
if(in_array($ID,$ids)){
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 ? " time" : " times";
echo "<br />\n";
}
else
echo "Link does not exist.";
}
?>
I call the link in IE like this: http://www.site.com/downloads.php?file=document.pdf - the code is for instance: /downloads.php?file=<?php $myLink = makeDownloadLink(13); ?> Why won't IE open the pdf just by clicking the link?
The weird thing is that I have this other force download and counter script, which uses flat files. It's much more simple, but here there's no problems at all. It looks like this:
<?php
$download_dir = '.'; // the folder where the files are stored
$counter_dir = 'counters'; // the folder where your counter files are stored
// each file to download must have a file called like "filename + .txt" in the counter folder
// download the file [download.php?get=name_of_file]
$path = $download_dir.'/'.$HTTP_GET_VARS['get'];
if(file_exists($path)){
$file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','r+');
$count = fread($file,100);
fclose($file); // closes file
$count++;
$file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','w'); // opens file again with 'w'-parameter
fwrite($file, $count);
fclose($file);
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$HTTP_GET_VARS['get']);
header('Content-Length: '.$size);
readfile($path);
}else{
echo "<font face=$textfont size=2>";
echo "<center><br><br>The file [<b>$get$extension</b>] is not available for download.<br>";
echo "Please contact the web administrator.";
}
?>
I use it linking like this: http://www.site.com/downloads.php?get=document.pdf IE has no problems with this link and opens any pdf - why is that? I know, I could just use this last script but I'd like to keep data in a database.
Thanks in advance.
Meek