force_download('101.mp3','Исполнитель - Композиция');
function tagSize($size_to_convert) {
$result = 0;
$mask = 0x7F;
for ($i = 0; $i < 4; ++$i) {
$temp = ($size_to_convert >> 7 * $i) & $mask;
$result |= ($temp << 8 * $i);
}
return $result + 10;
}
function force_download($file, $name) {
if (!file_exists($file)) die("Файл не найден!");
if (fopen($file, 'r') && ob_get_level()) {
ob_end_clean();
}
// $name должна быть в формате "Исполнитель - Композиция"
list($artist, $title) = explode(' - ', $name);
$comment = 'site.ru';
$artist = mb_convert_encoding($artist, 'cp1251', 'utf-8');
$title = mb_convert_encoding($title, 'cp1251', 'utf-8');
$comment = mb_convert_encoding($comment, 'cp1251', 'utf-8');
$tpe1 = pack('A4Nx3A*', 'TPE1', strlen($artist)+1, $artist);
$tit2 = pack('A4Nx3A*', 'TIT2', strlen($title)+1, $title);
$comm = pack('A4Nx7A*', 'COMM', strlen($comment)+5, $comment);
$idlength = strlen($tpe1.$tit2.$comm."\0");
$id3 = pack('A3vxN', 'ID3', 0x3, tagSize($idlength));
$length = filesize($file) + $idlength + 10;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$name.'.mp3');
header('Content-Length:'.$length);
print $id3.$tit2.$tpe1.$comm."\0".file_get_contents($file);
exit;
}