1

Being a new coder I keep doing a lot of trial and errors and stick with the finds that work, how ever the method I have at the moment does not work with all browsers.

Our PHP code.

$songs = file_get_contents('https://example.com/tracks/'.$id.'/');
$filename = './tmp/'.$SongTitle.'.mp3'; // used for the file_put_contents.
$filenames = '/tmp/'.$SongTitle.'.mp3'; // used for the echo
file_put_contents($filename, $songs);

HTML Download Button.

<div style="text-align: center; font-size: 20px;" class="testing-content">
<button type="button" class="btn btn-success"><a href="<?php echo $filenames; ?>" download='<?php echo $filenames; ?>' href="javascript:">Download</button></a>
</div>

You can see that I am using the Html5 and the Javascript method above meaning it only has support on FireFox and Google Chrome.

How can I allow users on all browsers to download the file on click of our button?

  • What browsers are you having issues with and what are those issues? See http://blog.deepbluesky.com/blog/-/browser-support-for-css3-and-html5_72/ – Wesley Smith Nov 26 '15 at 04:05
  • You can do this by sending headers :) – icecub Nov 26 '15 at 04:05
  • @icecub Could you provide an example? –  Nov 26 '15 at 04:05
  • Sure give me a few mins – icecub Nov 26 '15 at 04:06
  • @DelightedD0D The download button simply does not appear on browsers like Safari and Waterfox even have not tested any other browsers but with this method those are the two that I know for sure it's not working on. –  Nov 26 '15 at 04:06
  • @DelightedD0D Not a duplicate, already read over that post. –  Nov 26 '15 at 04:09

2 Answers2

3

The reason is FireFox and Google Chrome plays audio or video automatically for mime type octet stream, however IE does not have support for this.

You need to add .mp3 extension and mime type in server configuration to download your file in IE.

Ahmed
  • 184
  • 7
0

Alright, as promissed:

<?php

$file = "example.mp3";

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$file);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: audio/mp3");
readfile($file);

?>

Now just create a hyperlink or button that points to this php file.

EDIT: I should add that this is just an example as requested ofcourse! This method is very inefficient in its current form. If you plan on using multiple audio files, you should make the filename dynamicly through POST / GET.

icecub
  • 8,615
  • 6
  • 41
  • 70
  • Using this method it downloads an empty file, just as it did the first time I messed around with headers... I must be doing something wrong. –  Nov 26 '15 at 04:26
  • @Open Probebly. I've tested it here first and it works perfectly fine. Unfortunetely I can't help you with the issue because I don't have your code. You can either edit your question with it or click on my profile. You'll find my email address over there for more in depth help if required :) – icecub Nov 26 '15 at 04:28
  • I shot you an email! thanks. –  Nov 26 '15 at 04:34
  • @Open Got it. Send reply – icecub Nov 26 '15 at 04:38