下载按钮-将文件保存到磁盘,而不是打开


download button - save file to disk instead of opening

我有一个下载按钮,当我点击它时,它没有保存到磁盘,而是在浏览器中打开它。我试了很多次让它在浏览器中打开,但它似乎没有任何作用

<?php
// make a connection to the database
require_once("connection/connection.php");
//retrieve the ID from the url to fetch the specific details
if ($_GET['id'] != ""){
$item_id = $_GET['id'];
$bad_id = FALSE;    
}
else{
$item_id = "";
$bad_id = TRUE; 
}
//select the specific item from the database
// run if statement to ensure valid id was passed
if (is_numeric ($_GET['id'])){
$query = "SELECT name FROM repository WHERE item_id = '$item_id'";
$result = mysql_query($query) or die(mysql_error());
// assign the values to an array
$row = mysql_fetch_assoc($result);
//assign the values from the array to variables
$name = $row['name'];
}
// define path to the xml file
$file = "xml/".$hud_name . "_cfg.xml";
// check to make sure the file exists
if(!file_exists($file)){
    die('Error: File not found.');
} else{
    // Set headers
    header("Content-Type: application/xml");
    header("Content-Disposition:attachment; filename=".basename($file)."");
    readfile($file);
}
?>

这是download.php,它显然找到了该文件,因为它没有给出关于它不存在的错误。它还回显正确的文件路径

然后在另一页上我有:

<a href="download.php?id=<?php echo $item_id; ?>"><img src="images/download.png" alt=""/></a>

有什么想法吗?

最终解决方案很简单,但我没有看到任何文档说标题必须是第一行。如果我放置:

header("Content-Type: application/xml");

作为第一行,然后是它下面的编码和结尾的其他标题信息。我不确定这是解决方案还是变通方法,但它为我修复了