使用PHP强制下载MP3

Cha*_*ie_ 0 php mp3 download

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

我已经尝试过像这样的东西

<a href="direct_download.php?file=fineline.mp3">Download the mp3</a>
Run Code Online (Sandbox Code Playgroud)

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

href="<?php echo $val['stream_url']?>?client_id=67739332564a7130c3a05f90f2d02d2e">Descargar</a>.
Run Code Online (Sandbox Code Playgroud)

当我使用该选项时

direct_download.php?file=
Run Code Online (Sandbox Code Playgroud)

只需下载一个带有名称的文件

client_id=05b4f2000dad27aa9fc48d08915d7830.html
Run Code Online (Sandbox Code Playgroud)

我使用的完整的PHP代码是

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

可以在任何锚点访问,如下所示:

<a href="direct_download.php?file=fineline.mp3">Download the mp3</a>
Run Code Online (Sandbox Code Playgroud)

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

Isa*_*aac 9

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

<a href="url_to_your_file.mp3" download>Download the mp3</a>
Run Code Online (Sandbox Code Playgroud)

否则,你可以使用javascript

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();
}
Run Code Online (Sandbox Code Playgroud)

从您的链接中调用它

<a href="javascript:" onclick="saveAs(url_to_your_file.mp3)">Download the mp3</a>
Run Code Online (Sandbox Code Playgroud)