使用PHP强制下载MP3

编程入门 行业动态 更新时间:2024-10-25 16:19:39
本文介绍了使用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

本文发布于:2023-11-01 12:07:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!