Mr.Nikan Mr.Nikan - 7 months ago 22
PHP Question

forcing to download mp3 link instead of streaming it

i been looking and i could not find a way to force to save a mp3 file ( from link )
i found this that works with the files on same host Like

x.mp3
or a/x.mp3

<?php
$file = $_GET['file'];
header("Content-type: octet/stream");
header("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>


but how can i do this for mp3 links which stream instead of downloading it ?
like

http://ahmusm.mubox.in/nevisande/aliz/mp3/1395/02/05/Beyonce%20-%20Hold%20Up.mp3


thanks in advance

Answer

In HTML use:

<a download href="file.mp3">

In modern browsers it forces download.

This also allows you to avoid using PHP to serve that particular file. By default PHP prevents download resumption and makes serving relatively expensive for your server.