使用PHP强制下载MP3 [英] Force Download MP3 with PHP

查看:67
本文介绍了使用PHP强制下载MP3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Be honest : I found the way to donwload mp3 files from s******d but Im not able to donwload the MP3 directly all the music is streaming in the browser.

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

I already try with someting like this

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

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

but my donwload link is not .mp3, this is my line of code to donwload a file

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

当我使用该选项时

direct_download.php?file=

仅下载一个名称为

client_id=05b4f2000dad27aa9fc48d08915d7830.html

我使用的完整的php代码是

The complete php code that I use is

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

可以在任何锚点中访问以下内容:

Which can be accessed in any anchor like this:

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

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

Could you please help me, or if you have a better ide, thank you guys

推荐答案

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

if you are using html5 you can use download option

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

否则,您可以使用javascript

otherwise, you can use 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();
}

从您的链接中调用

<a href="javascript:" onclick="saveAs(url_to_your_file.mp3)">Download the mp3</a>

这篇关于使用PHP强制下载MP3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆