php 用php输出mp3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5275126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:50:08  来源:igfitidea点击:

Output mp3 with php

phpheaderstreammp3

提问by Tim S.

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 输出一个 mp3 文件,但显然它只是输出了一些损坏的文件,我不知道为什么。我浏览了整个网络以找到解决方案,但没有。

<?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!

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

采纳答案by Tim S.

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)

嗯,答案还很遥远,这不是我的剧本。在我加载所有内容的任何 HTML 标签之前,如果定义了自定义标题,我只显示内容(从模板中检索)

<?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; ?>

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.

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

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; ?>

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

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

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

谢谢你们,肯定会给你竖起大拇指,因为它提供了非常有用的信息!

回答by Furicane

header('Content-Disposition: inline;filename="test.mp3"');

That's what you're missing in your header() (you had no "inline" specified). Content-Transfer-Encoding should be "binary", not "chunked".

这就是您在 header() 中缺少的内容(您没有指定“内联”)。内容传输编码应该是“二进制的”,而不是“分块的”。

Edit: use "inline" if you want it to be displayed within the browser (if capabilities exist) or "attachment" if you want to force download.

编辑:如果您希望它在浏览器中显示(如果功能存在)或“附件”如果您想强制下载,请使用“内联”。

回答by osm

can you try replace

你可以尝试更换吗

header("Content-Transfer-Encoding: binary");
符合
header("Content-Transfer-Encoding: chunked");