如何在php和mysql中建立下载链接

时间:2015-02-07 07:22:41

标签: 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脚本:

<?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;
    }
?>

1 个答案:

答案 0 :(得分:0)

这是简单的例子

$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=3 http://www.finalwebsites.com/forums/topic/php-file-download