By using the mime type audio/mpeg you tell the browser to "do your default action with this file". In example if you have an jpg file and set the mime type to image/jpeg the browser will read the jpg and display it inside the browser window.
The solution is to use the mime type application/data instead. This will download the file leaving the browser out of it.
That would be
header("Content-type: application/data");
== Updated ==
A more complete approach
header("Content-type: application/data");
header("Content-Disposition: attachment; filename=$this->filename");
header("Content-Description: PHP Generated Data");
readfile($this->file);
If you want a dynamic mime type reader you could use this
$type = $this->get_mime_type($this->filename);
header("Content-type: " . $type);
...
private function get_mime_type($filename) {
$fileext = substr(strrchr($filename, '.'), 1);
if (empty($fileext)) {
return (false);
}
$regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileext)/i";
$lines = file("mime.types", FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
if (substr($line, 0, 1) == '#') {
continue;
}
if (!preg_match($regex, $line, $matches)) {
continue;
}
return ($matches[1]);
}
return ("application/data");
}
And, to get a list of mime types you can check my lab version, save the displayed content and save it to a file named mime.types in the root of your website.
http://www.trikks.com/lab/mime.html
Have fun