Problemer med url i download-script i IE
Hej.Jeg har dette force-download script, der samtidig tæller antal downloads.
<?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.dk';
$config['file_path']=$_SERVER['DOCUMENT_ROOT'].'/minMappe';
$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);
}
?>
Det virker perfekt, og så alligevel ikke. Når man blot klikker på et link, f.eks. <a href="downloads.php?file=<?php $myLink = makeDownloadLink(6); ?><?php print($myLink[0]); ?>">Download</a> så er alt i skønneste orden: Filen hentes og der tælles op. Hvis man derimod skriver hele url'en i Internet Explorer: http://www.site.com/downloads.php?file=doc.pdf så melder IE at den ikke kan indlæse filen og at det anmodede websted ikke er tilgængeligt. I Firefox virker det fint. Kan man gøre noget for at få den direkte url til at virke i IE? Kan det være noget med headers?
Mvh.
