我使用
PHP脚本提供从我的网站下载必要的javascript计时器这个PHP脚本包含导致下载.但无论我尝试什么,下载的文件都会损坏.任何人都可以帮我指出我哪里出错了.
这是我的代码
include "db.PHP";
$id = htmlspecialchars($_GET['id']);
$error = false;
$conn = MysqL_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!($conn)) echo "Failed To Connect To The Database!";
else{
if(MysqL_select_db(DB_NAME,$conn)){
$qry = "SELECT Link FROM downloads WHERE ID=$id";
try{
$result = MysqL_query($qry);
if(MysqL_num_rows($result)==1){
while($rows = MysqL_fetch_array($result)){
$f=$rows['Link'];
}
//pathinfo returns an array of information
$path = pathinfo($f);
//basename say the filename+extension
$n = $path['basename'];
//NOW comes the action,this statement would say that WHATEVER output given by the script is given in form of an octet-stream,or else to make it easy an application or downloadable
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($f));
//This would be the one to rename the file
header('Content-Disposition: attachment; filename='.$n.'');
//Finally it reads the file and prepare the output
readfile($f);
exit();
}else $error = true;
}catch(Exception $e){
$error = true;
}
if($error)
{
header("Status: 404 Not Found");
}
}
}
?>