用php输出mp3

编程入门 行业动态 更新时间:2024-10-25 14:23:17
本文介绍了用php输出mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习我的学校项目,我试图用PHP输出一个mp3文件,但显然它只是输出一些损坏的文件,我不知道为什么。我浏览了整个网络以找到解决方案,但是没有。

I'm working on my school project and I'm trying to output a mp3 file using PHP but apparently it just outputs some corrupt file and I have no clue why. I browsed the entire net to find a solution, but no.

<?php $filename = 'audio/1/1.mp3'; if(file_exists($filename)) { header('Content-Type: audio/mpeg'); header('Content-Disposition: filename="test.mp3"'); header('Content-length: '.filesize($filename)); header('Cache-Control: no-cache'); header("Content-Transfer-Encoding: chunked"); readfile($filename); } else { header("HTTP/1.0 404 Not Found"); } ?>

任何人都可以向我解释这一点吗?那真是太棒了!

Can anyone explain this to me? That would be totally awesome!

推荐答案

好的,答案很简单,它不是我的脚本。在加载任何HTML标签之前,我加载了所有内容,如果定义了自定义标题,我只会显示内容(从模板中检索到)。

Well the answer was way off and it wasn't my script. Before any HTML tag I load all content, if custom headers are defined I only show the content (retrieved from templates)

<?php //get stuff if(empty($page->customHeader)): ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "www.w3/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <html xmlns="www.w3/1999/xhtml"> <head> <title><?php print $page->title ?></title> </head> <body> <div class="container"> <div class="header"> </div> <div class="content"> <?php print $page->content ?> </div> <div class="footer"> </div> </div> </body> </html> <?php else: print $page->content; endif; ?>

对于模板,我使用ob_get_contents(),所以在实际的模板中我回显/打印数据。出于某种原因,ob_get_contents()不能正确提取,所以这是错误的地方。

For the templates i use ob_get_contents(), so in the actual template I echo/print data. For some reason ob_get_contents() doesn't fetch that properly so that's where it went wrong.

现在我定义数组中的标题和另一个内容的路径变量并调用它。

Now I define my headers in an array and the path of the content in another variable and call that.

... </html> <?php else: foreach($page->customHeader as $header) { header($header, true); } readfile($page->headerContent); endif; ?>

长话短说: ob_get_contents()不会获得二进制内容。

Long story short: ob_get_contents() doesn't get binary content.

非常感谢你们,肯定会给你大量的信息和相当有用的信息!

Thanks guys, definately going to give you thumbs up for the efford and rather useful information!

更多推荐

用php输出mp3

本文发布于:2023-11-01 12:06:56,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:php

发布评论

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

>www.elefans.com

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