PHP Streaming MP3
Solution 1:
Here's what did the trick.
$dir = dirname($_SERVER['DOCUMENT_ROOT'])."/protected_content";
$filename = $_GET['file'];
$file = $dir."/".$filename;
$extension = "mp3";
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
if(file_exists($file)){
header('Content-type: {$mime_type}');
header('Content-length: ' . filesize($file));
header('Content-Disposition: filename="' . $filename);
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($file);
}else{
header("HTTP/1.0 404 Not Found");
}
Solution 2:
You can try HTTP chunking. Set the "Transfer-Encoding" header to "chunked", then output the size of each chunk before sending it. End each chunk size and chunk with a CRLF.
For anything more complex, I recommend using a streaming server, such as Icecast.
Solution 3:
Two things stand out:
- You've got a
Content-Length
set. If you server is set to automatically gzip your output, this can mess with things. Try turning offContent-Length
and see if that fixes it. - You've got about a thousand
Content-Type
s set. Since it's Mp3 that you're serving, just useaudio/mpeg
. You can effectively get rid of the whole lastheader()
command. It's easy to get carried away with HTTP headers.
Try it out and let us know how it goes!