Force a link to download an MP3 rather than play it?

Discussion in 'Web Design and Development' started by Mr Skills, Sep 18, 2008.

  1. macrumors 6502a

    Mr Skills

    Joined:
    Nov 21, 2005
    #1
    I have put some free MP3s on my band's website (link in the sig) and hosted them on idisk, but when I click on them they play rather than downloading. It feels inelegant to zip them or ask people to right-click.

    All the searches I have done on this topic suggest that it's just tough luck BUT.... if I host the MP3s on garageband.com they download, so they have obviously found some way round it.

    website download link

    garageband.com download link

    The only obvious difference is that on garageband the song has "?|pe1|S8LTM0LdsaSgYFi3Y2k" after the filename on the link. Also, I've seen talk of changing MIME types so the browser doesn't know it's an MP3. Is this what I should do, and if so, how?

    Thanks! :)
     
  2. macrumors regular

    Joined:
    Feb 29, 2004
    #2
    You have to change the content type in the header to force download. This will take some server side scripting. This is how i would do it in PHP, the application part of the content type forces it to be downloaded.

    Code:
    <?php
    // We'll be outputting a MP3
    header('Content-type: application/mp3');
    // It will be called file.mp3
    header('Content-Disposition: attachment; filename=file.mp3');
    header('Content-Length: '.filesize("somefile.mp3"));
    // The MP3 source is in somefile.pdf
    //readfile("somefile.mp3");
    readfile_chunked("somefilename.pdf");
    
    function readfile_chunked($filename){ 
     $chunksize = 1*(1024*1024); // how many bytes per chunk 
     $buffer = ''; 
     $handle = fopen($filename, 'rb'); 
     if ($handle === false) { 
     return false; 
     } 
     while (!feof($handle)) { 
       $buffer = fread($handle, $chunksize); 
     print $buffer; 
     } 
     return fclose($handle); 
     } 
    ?>
     
  3. thread starter macrumors 6502a

    Mr Skills

    Joined:
    Nov 21, 2005
    #3
    Thanks msheperd - the only problem (apart from my minimal paint-by-numbers HTML ability!) is that the website is actually a Blogger page, and the links are just HTML within a blog post... so I don't think I've got any server-side access.

    Is it possible to put such headers/php within an HTML widget?
     
  4. macrumors regular

    Joined:
    Feb 29, 2004
    #4
    the only thing you may be able to do is change the mime types within the apache config if you have access to that.
     
  5. macrumors regular

    Joined:
    Aug 20, 2007
    Location:
    In the snow with Rosebud
    #5
    In this case, since you say you don't have too much html/scripting experience, it might be best to just give your users instructions.

    "Right click the links below and select 'Save Target As' to download to your hard drive" or something like that.

    This will give them a choice--some people might rather stream it. Or this could be an opportunity to delve even deeper into the wonderful world o' web development. Good luck.
     
  6. macrumors newbie

    Joined:
    Dec 10, 2007
    #6
    Force a link to download an MP3 rather than play it?

    Use FTP intead of HTTP as the source link, this should bring up the save / open dialog box.
     
  7. thread starter macrumors 6502a

    Mr Skills

    Joined:
    Nov 21, 2005
    #7
    It's a blogger blog, which just lets you put HTML into a box. So I don't think I've got access to things like Apache config (which I assume would be on the server).

    Well I know a little HTML... enough to follow instructions. It's just that it has to be relatively simple HTML/Javascript because it's going in a blogger widget. I've already got the "save as" instruction on there, but it's just so inelegant, especially since it's a slightly different instruction on every browser! (Which reminds me... bizarrely, sometimes you right-click on a song in Safari, the menu says "download linked file" and sometimes it says "save as" but it changes when I reload the page...)

    Good suggestion... but it didn't work. The browser was just endlessly loading (maybe because iDisk is webdav and not FTP?).

    Thanks for all the suggestions so far... keep them coming! :)
     
  8. macrumors regular

    Joined:
    Aug 20, 2007
    Location:
    In the snow with Rosebud
    #8
    This is true. Frankly I don't know the exact solution to your problem. Maybe if you zipped the mp3 before uploading to your blog. Then the browser won't be able to stream it. They'll have to download it and unzip it themselves (I think). Might work...
     
  9. macrumors newbie

    Joined:
    Jan 27, 2015
    #9

Share This Page