MP3 output from php

I am working on my school project and I am trying to output an mp3 file using PHP, but apparently it just outputs some kind of corrupt file, and I don't know why. I scanned the entire network 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 someone explain this to me? That would be awesome!

+5
source share
3 answers

Well, the answer was gone, and that was not my script. Before any HTML tag, I load all the content, if custom headers are defined, I only show the content (extracted from the 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() , , .

.

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

    readfile($page->headerContent);
endif; ?>

: ob_get_contents() .

, , !

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

, () ( "inline" ). Content-Transfer-Encoding "", "chunked".

: "", , ( ) "", .

+6

try replacing the string

header ("Content-Transfer-Encoding: binary");
on the
header ("Content-Transfer-Encoding: chunked"); 
0
source

All Articles