用php输出mp3 [英] Output mp3 with php

查看:912
本文介绍了用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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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