2015-02-07 95 views
-2

我製作一個下載歌曲文件的頁面。但我運行該頁面並單擊鏈接,然後沒有發生。 我不知道我在做什麼,請解釋我做了什麼。如何在php和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; 
    } 
?> 
+0

嘗試[演示和腳本(http://www.finalwebsites.com/forums/topic/php-file-download)或[教程](HTTP:// WWW。 media-division.com/the-right-way-to-handle-file-downloads-in-php/) – 2015-02-07 07:31:27

回答

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