I want to download a single .mp3 file from my site but when using this code it forces a .php in Firefox and Safari. But in chrome it will send force the file as inline and play on the page. How can i get them to actually download a .mp3 file?
$track = $_SERVER['QUERY_STRING'];
if (file_exists("/home/user/gets/" .$track)) {
header("Content-Type: audio/mpeg");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: attachment; filename="test.mp3"');
$str = "/home/user/gets/".$track;
readfile($str);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
I have also tried to download a .zip file as well and changing the Content-Type to application/ocetet-stream but it forces .php files on all browsers.
//$track = $_SERVER['QUERY_STRING'];
$track = 'testfile.zip';
if (file_exists("/home/user/gets/" .$track)) {
header("Content-Type: application/octet-stream");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: attachment; filename="test.mp3"');
$str = "/home/user/gets/".$track;
readfile($str);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
I think filesize($track)
is wrong, it should be the whole path filesize("/home/user/gets/".$track)
. This would cause php to output error messages, preventing you from setting the content-length and disposition header.