我正在学习我的学校项目,我试图用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
发布评论