Hi,
I have this force download script, which also counts no. of downloads. It works fine yet there's a problem with it.
<?php
/*
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($_SERVER['DOCUMENT_ROOT']."/scripts/sql.php");
$config=array();
$config['email'] = 'webmaster@mail.com';
$config['file_path']=$_SERVER['DOCUMENT_ROOT'].'/myFolder';
$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.
function makeDownloadLink($id){
global $conn;
$result = mysql_query("SELECT name, downloads FROM downloads WHERE ID='$id'",$conn);
$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);
}
?>
Whenever I make a link on my page like this -
<a href="downloads.php?file=<?php $myLink = makeDownloadLink(6); ?><?php print($myLink[0]); ?>">Download</a>
- it works just fine. The file is downloaded and the count increments by one. However, if I put the entire url in Internet Explorer like this: http://www.site.com/downloads.php?file=document.pdf I get an error saying that IE can't read the file and that the requested website is unavaliable. Funny thing is, in Firefox the url works fine. Any ideas why that is? Does it have to do with the headers in the script or...?
Thanks in advance.