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


how to make download link in php and mysql

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

这是我的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;
}

和教程

http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/http://www.onlamp.com/pub/a/php/2000/09/15/php_mysql.html?page=3http://www.finalwebsites.com/forums/topic/php-file-download