如何在php和mysql中制作下载链接

如何在php和mysql中制作下载链接,php,html,Php,Html,我制作了一个下载歌曲文件的页面。但我运行那个页面,点击链接,然后什么也没发生。 我不知道我在做什么,请给我解释一下我在做什么 这是我的html和php 文件代码 <html> <head> <meta charset="utf-8"> </head> <body> <a href="download.php">click here for download song</a> </body> <

我制作了一个下载歌曲文件的页面。但我运行那个页面,点击链接,然后什么也没发生。 我不知道我在做什么,请给我解释一下我在做什么

这是我的html和php

文件代码

<html>
<head>
<meta charset="utf-8">

</head>

<body>
<a href="download.php">click here for download song</a>
</body>
</html>

php脚本:

<?php

$conn =mysqli_connect("localhost","root","","test1");
$sql="select song_name,song_path from table1 where id=11";

while($row=mysqli_query($conn,$sql)){


    echo $name_of_file = $row["song_name"];
    $FilePath = $row["song_path"];
    echo $size_of_file = filesize($FilePath);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' .$name_of_file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);

echo $FilePath;
    }
?>

下面是一个简单的例子

$file = 'C:/xampp/htdocs/intranet/www/public/uploads/'.$file_name;

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
和教程

试试看