Log in

View Full Version : Get.php? - Help downloading files...



DigiplayStudios
03-05-2010, 10:06 PM
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? :confused:

djr33
03-06-2010, 06:39 PM
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.

bluewalrus
03-06-2010, 06:53 PM
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?

DigiplayStudios
03-06-2010, 07:07 PM
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

james438
03-06-2010, 08:23 PM
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.

djr33
03-06-2010, 11:16 PM
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.

DigiplayStudios
03-07-2010, 10:36 AM
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? :)

DigiplayStudios
03-07-2010, 10:40 AM
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) -->

http://img412.imageshack.us/img412/6640/screenbx.png

djr33
03-07-2010, 11:02 AM
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.