MP3 File download in server from URL

Example how to download MP3 (or other) file in server from URL.


<?php
$url = 'http://site.com/sound.mp3';
 
//Sound name.
$name = 'sound_name.mp3';
 
//Get file.
$data = get_headers($url, true);
$size = isset($data['Content-Length'])?(int) $data['Content-Length']:0;
 
ob_get_level() && ob_end_clean();
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
header('Content-Type: application/force-download');
header('Content-Description: inline; File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename=' . $name, false);
header('Content-Length: ' . $size);
 
$upload = new SplFileObject($url, 'rb');
while (!$upload->eof()) {
    echo($upload->fgets());
}
 
 
flush();
exit;
?>