使用PHP从MySQL下载文档

使用PHP从MySQL下载文档,php,mysql,Php,Mysql,由于某种原因,在我点击url下载后,它碰巧在我的浏览器上查看了该文件,结果显示了大量的符号和随机字母。有没有办法让它下载而不是在浏览器上查看?我对编程很陌生,你的指导会很有帮助的。多谢各位 <?php mysql_connect("localhost","root",""); mysql_select_db("tuitioncentre"); if(isset($_GET['id'])) { // if id is set then get the file with the id

由于某种原因,在我点击url下载后,它碰巧在我的浏览器上查看了该文件,结果显示了大量的符号和随机字母。有没有办法让它下载而不是在浏览器上查看?我对编程很陌生,你的指导会很有帮助的。多谢各位

<?php

mysql_connect("localhost","root","");

mysql_select_db("tuitioncentre");

if(isset($_GET['id'])) { // if id is set then get the file with the id from database

$id = $_GET['id'];

$query = "SELECT name, type, size, content FROM files WHERE id = $id";

$result = mysql_query($query) or die('Error, query failed');

list($name, $type, $size, $content) =

mysql_fetch_array($result);

header("Content-length: $size");

header("Content-type: $type");

header("Content-Disposition: attachment; filename=$name");

echo $content; exit;

}

?>



<?php

$query = "SELECT id, name FROM files";

$result = mysql_query($query) or die('Error, query failed');

if(mysql_num_rows($result) == 0)

{

echo "Database is empty";

}

else

{

while(list($id, $name) = mysql_fetch_array($result))

{

?>

<a href="download2.php?id=<?php echo $id;?>"><?php echo $name; ?></a>

<?php

}

}

这是输出的示例。

似乎您设置的类型不正确,您的数据库中有什么内容?看在上帝的份上,不要使用mysql_*函数,切换到PDO。我的数据库中的类型是application/msword。在你的浏览器中,响应头是什么?我问这个问题是因为我怀疑您之前有一些输出,因此标题被忽略,输出被发送到浏览器。输出可以像单个空格一样简单。警告:无法修改标题信息-已发送的标题(输出从第145行开始)第145行是标题(“内容长度:$size”);注释掉你的标题语句和
echo$内容说了这么多,你的浏览器还剩下什么?