我在自己的网站上工作,用户可以按下载按钮并下载音乐文件。用户选择文件格式如下:
<div id="choose_format"><p>Select Download Format:</p>
<select id='download_format' name="download_format">
<option value="1">--SELECT--</option>
<option value="m4r">M4r (Apple iPhone)</option>
<option value="mp3">Mp3 (Android)</option>
</select>
</div>
然后他们按下一个下载按钮,这是单击下载按钮时调用的功能:
function download(song_id){
var download_format = document.getElementById('download_format').value;
if(download_format == '1'){
alert("Please select a download format from the dropdown menu");
$('#download_format').addClass('highlight_box');
}else{
var product_id = 'productID' + song_id;
var productID = document.getElementById(product_id).value;
window.location.href = "download_tone3.php?download_format=" + download_format + "&productID=" + productID;
//xmlhttp_down = createXHR();
//xmlhttp_down.onreadystatechange = down_callback;
//xmlhttp_down.open("POST", "download_tone3.php" ,true);
//xmlhttp_down.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//xmlhttp_down.send("download_format=" + download_format + "&productID=" + productID);
}
}
在download_tone3.php中我想连接到我的音乐数据库,这样我就可以将音乐文件的产品ID与数据库进行比较,以检索正确的下载链接。所以我有以下php文件download_tone3.php:
<?php
require ("../../identity/music_connect.php");
$productID = $_REQUEST['productID'];
$download_format = $_REQUEST['download_format'];
$GLOBALS['download_link'] = '';
$result = mysqli_query($con,"SELECT * FROM products WHERE ProductID=" . $productID);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row = mysqli_fetch_array($result)){
if($download_format == 'm4r'){
$GLOBALS['download_link'] = $row['Productm4r'];
}else if($download_format == 'mp3'){
$GLOBALS['download_link'] = $row['Productmp3'];
}
}
$mimeTypes = array(
'mp3' => 'audio/mpeg',
'm4r' => 'audio/x-m4r',
);
// set the file here (best of using a $_GET[])
$file = "../" . $GLOBALS['download_link'];
//gets the extension of the file to be loaded for searching array above
$ext = explode('.', $file);
$ext = end($ext);
// gets the file name to send to the browser to force download of file
$fileName = explode("/", $file);
$fileName = end($fileName);
// opens the file for reading and sends headers to browser
$fp = fopen($file,"r") ;
header("Content-Type: ".$mimeTypes[$ext]);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
// reads file and send the raw code to browser
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
//file_put_contents('media/audio/ringtones/anthem-sarah_monks.' . $download_format,$buff);
}
// closes file after whe have finished reading it
fclose($fp);
//mysqli_close($con);
?>
这个php文件非常适合强制下载文件,直到我试图要求另一个php文件并使用数据库。它抛出一个错误: &#34;无法修改标题信息 - 已经发送的标题(输出开始于/home4/monksee/identity/music_connect.php:15)"谢谢你的帮助。
答案 0 :(得分:1)
你的/music_connect.php是echo / print的东西。看看这个。