Results 1 to 9 of 9

Thread: Get.php? - Help downloading files...

  1. #1
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Get.php? - Help downloading files...

    Hi There,

    I run a website which links to thousands of external .MP3 files. At the moment the links simply go directly to the URL of the .MP3 file which means the browsers act in different ways, some playing the MP3 files with their plugins, others saving the files, ect.

    How do I create a get.php file that gets the .MP3 from the external website and opens a 'Save As...' dialogue box?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are several answers to this.

    1. page.php?song=name.mp3
    That will get you $_GET['song'] = 'name.mp3' on the "get.php" page.
    Then you just do readfile($_GET['song'])
    Also, make sure you validate this path. For example, either have a list of OK songs (and check that the requested song is in there), or make sure that there are no '/' characters in the path, etc.
    2. When you use readfile() it outputs the file directly. There are a few other methods also. But the main idea here is that you will also need to submit file format headers so that browser knows what to do with it. In other words, if you do nothing, it will act like it's a broken PHP page. If you set it correctly it will serve as an mp3 or jpg or whatever.
    3. It is NOT possible to force a "save as..." dialog box. However, you can try to trick the browser by sending a header that doesn't make sense like "force-download" (google "force download header php"), but that doesn't actually DO anything-- it just doesn't make sense and thus the browser defaults to saving it rather than opening it. This can also cause unexpected results in Internet Explorer sometimes because it will try to guess the content rather than "listening" to the header you tell it. Regardless, that's basically your only option though it has some downsides.

    Note that (2) and (3) contradict each other, so consider that while planning your project.

    Some alternatives:
    1. Just tell your visitors to choose "right click, save as" to download the file. This may help in addition to another method if, for example, internet explorer does something strange.
    2. The only non-trick way to make this happen is to use a filetype that cannot be opened by the browser. The simple answer then is to use a zip file. While it will take a little more work, this means that you can create zip files that will save a bit of bandwidth (not that much on mp3s but a little), then force the user to download it every time, but also have to unzip before playing. Tradeoff, but reliable.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Wouldn't just changing the extension to .zip by text (not winzip or an archive program) work and telling the user to switch it to .mp3, or does the browser actually look at the encoding of the file?
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    The issue I have is with Internet Explorer, it really doesn't want to read the .MP3 files as audio files but instead wants to do it's own thing and read them in a text format! :S

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    what is the code you are using and what version of internet explorer? I am unable to repeat the problem on this end. I use ie8 though.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That sounds like the wrong headers are being sent. Use PHP and set the headers to the correct ones for mp3 (search for those-- they won't be hard to find). If IE doesn't ignore those headers, that will work. If not, it's best to zip it, I think.


    @bluewalrus, that's not a good idea because many users would be very confused by that and it's rarely a good idea to "pretend" something is in a certain file format. If the user just clicks the file, winzip (or another program) will tell them it's corrupt and they will delete it and leave the site. It's better in that case, since there's still work involved for the user, to just tell them to right click save as.
    However, there are ways to create zip files using automated methods especially if you're on a linux server.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    DigiplayStudios (03-07-2010)

  8. #7
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    That sounds like the wrong headers are being sent. Use PHP and set the headers to the correct ones for mp3 (search for those-- they won't be hard to find). If IE doesn't ignore those headers, that will work.
    Sorry, still fairly new to PHP! I take it I put the header codes in the page that links to the .MP3 file?

  9. #8
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    An update.. I try with IE8 and this is what appears when I open the .MP3 file (All the .MP3 files that I link to are on external websites) -->


  10. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Based on a quick search, it looks like this line will work:
    header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) file

    Use this to set the content-type to the right one for mp3.


    Just make sure that this goes before any output to the page. (since headers are sent before text or will cause errors)


    Also, if you have anything aside from the actual mp3 data, this could cause problems also, like a heading ("My mp3: ....") or error message.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •