使用PHP强制下载MP3

时间:2015-07-16 23:24:45

标签: php download mp3

说实话:我找到了从s ****** d下载mp3文件的方法但是我无法直接下载所有音乐在浏览器中传输的MP3。

我已经尝试过这样的事了

<a href="direct_download.php?file=fineline.mp3">Download the mp3</a>

但我的donwload链接不是.mp3,这是我下载文件的代码行

href="<?php echo $val['stream_url']?>?client_id=67739332564a7130c3a05f90f2d02d2e">Descargar</a>.

当我使用选项

direct_download.php?file=

只需下载一个名为

的文件
client_id=05b4f2000dad27aa9fc48d08915d7830.html

我使用的完整php代码是

<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

可以在任何锚点中访问:

<a href="direct_download.php?file=fineline.mp3">Download the mp3</a>

你能帮助我吗,或者如果你有更好的想法,谢谢你们

1 个答案:

答案 0 :(得分:9)

如果您使用的是html5,则可以使用下载选项

 var iframedoc = $('#if').contents()[0];
 iframedoc.open();
 iframedoc.writeln("<html><head></head><body><scr" + "ipt>" + 
  "var h1 = document.createElement('h1'); " + 
  "h1.innerText = 'hi!';" +  
  "document.body.appendChild(h1);" +
"</scri" + "pt></body></html>");
iframedoc.close();

否则,您可以使用javascript

<a href="url_to_your_file.mp3" download>Download the mp3</a>

从您的链接中调用

function saveAs(url) {    
  var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';
  xhr.onload = function() {
    var a = document.createElement('a');
    a.href = window.URL.createObjectURL(xhr.response);
    a.download = filename; 
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
    delete a;
  };
  xhr.open('GET', url);
  xhr.send();
}
相关问题