Send mp3 download to browser

Hello, please help me solve the problem: there are mp3 files that are submitted for download using header('Location: '.$mp3url);It’s fine in all browsers, but in Chrome the file is not served for download, but opens for playback in the browser itself, changed the method of sending to this

header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3, audio/mp3");
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$mp3name);
header('Content-Length: ' . filesize($mp3url));
readfile($mp3url);

So everything is fine on the download and in Chrome, but here is another problem: some mp3 files are not played, at least in Windows XP, the reason is not on the computer, I tried it on the other one in the same way.


Answer 1, authority 100%

Usually this is enough code :

$f = 'filename';//  ,   ,   4-    : filename=$f
$df = 'filepath';//  mp3
header('Content-Type: audio/mpeg');
header("Content-Disposition: attachment; filename=$f.mp3");
echo file_get_contents($df);
exit;

Please see the code :

<?php
set_time_limit(0);
function file_download($filename, $mimetype='audio/mpeg') {
    $download_speed =   51200; // 51200
    $time_discret   =   1;
    if (file_exists($filename)) {
        $f              =   fopen($filename, 'r');
        if (isset($_SERVER['HTTP_RANGE'])) {
            $load_from      =   preg_replace('#[^0-9]#', '', $_SERVER['HTTP_RANGE']);
            fseek($f, $load_from);
            $filesize       =   filesize($filename);
            header('HTTP/1.1 206 Partial Content');
            header('Content-Type: ' . $mimetype);
            header('Content-Range: bytes '.$load_from.'-'.$filesize.'/'.$filesize);
            header('Last-Modified: ' . gmdate('r', filemtime($filename)));
            header('ETag: ' . sprintf('%x-%x-%x', fileinode($filename), filesize($filename), filemtime($filename)));
            header('Accept-Ranges: bytes');
            header('Content-Length: ' . (filesize($filename)));
            header('Connection: close');
            header('Content-Disposition: attachment; filename="' . basename($filename) . '";');
        } else {
            header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
            header('Content-Type: ' . $mimetype);
            header('Last-Modified: ' . gmdate('r', filemtime($filename)));
            header('ETag: ' . sprintf('%x-%x-%x', fileinode($filename), filesize($filename), filemtime($filename)));
            header('Content-Length: ' . (filesize($filename)));
            header('Connection: close');
            header('Content-Disposition: attachment; filename="' . basename($filename) . '";');
        }
            if((int) $download_speed > 0) {
                while(!feof($f)) {
                    $time_start = microtime(true);
                    echo fread($f, ceil($download_speed*$time_discret));
                    flush();
                    $time_end = microtime(true);
                    $time = $time_end - $time_start;
                    if($time_discret-$time > 0) usleep(($time_discret-$time)*1000000);
                }
            } else {
                while(!feof($f)) {
                    echo fread($f, 1024);
                    flush();
                }
            }
        fclose($f);
    } else {
        header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
        header('Status: 404 Not Found');
    }
    exit;
}
#
file_download('./20_dt8_project.mp3', $mimetype='audio/mpeg');
?>

I don’t have time to help you right now, unfortunately. The code streams the file at the specified speed and supports resuming.