I\'m working on a way to serve up MP3 files through PHP and after some help form the SO massive, I got it working here
However, that example doesn\'t appear to work
You could simply make it so that you have a mod_rewrite rule to run every request for music/*.mp3 through your mp3.php file.
For example, something like this
RewriteEngine on
RewriteRule ^/music/(.*\.mp3) /music/mp3.php?file=$1 [L]
mp3.php can then pick up the requested file from $_GET['file'], but if you go with this approach I recommend you sanity check the filename, to ensure it only references a file in the desired directory.
//ensure filename just uses alphanumerics and underscore
if (preg_match('/^[a-z0-9_]+\.mp3$/i', $_GET['file']))
{
//check file exists and serve it
$track=$_SERVER['DOCUMENT_ROOT'].'/music/'.$_GET['file'];
if(file_exists($track))
{
header("Content-Type: audio/mpeg");
header('Content-length: ' . filesize($track));
//insert any other required headers...
//send data
readfile($track);
}
else
{
//filename OK, but just not here
header("HTTP/1.0 404 Not Found");
}
}
else
{
//bad request
header("HTTP/1.0 400 Forbidden");
}