This is a demo page that shows how the getmp3.php script works. Download the getmp3.php script here.
Test1 - here I link directly to the mp3.
Test2 - here I link through getmp3.php.
The second one forces a "Save File as" window to pop up (it may be have other names on different browsers). The first one does not.
Sometimes, when you click on an mp3 file on a webpage, it plays immediately without giving you an opportunity to save it. On certain sites, this may be deliberate. They just do not want you to save the mp3. However, it is sometimes unintentional. On many sites, the biggest constraint is the bandwidth. They prefer that you download the file once and save it on your hard disk. Not go back, again and again, everytime you want to listen to it.
I saw this post at http://www.ozoneasylum.com/28099
I have a few things on my personal web space that I want other people to be able to download, eg a pdf or mp3 file. If I just type the web address of these files then the pdf will open or the mp3 will play, so how can I get them to download instead so the user has the option to run or save them...?
Instead of linking directly to the file, you link to a script that sends a special header, and then, followed by the file. The header is
Content-disposition: attachment; filename="yourfile.mp3"This tells the browser that the file is an attachment and should be saved on the hard disk. The filename tells the browser what name to save it as. The purpose of the getmp3.php script is to send that special header. That's all it does.
Put the getmp3.php script in the directory where your mp3 files are. The link to download the mp3 files are
<a href=getmp3.php?f=yourfile.mp3>Yourfile.mp3</a>Just change yourfile.mp3 to whatever your mp3 files are actually named. Needless to say, your website must be able to run php scripts.
1. Does it work with other (non-mp3) files?
Yes it does.
You may want to open up the getmp3.php and change the line that says Content-type: audio/x-mpeg to whatever it should be. But it still works whether you change that or not.
2. Can I have spaces in my filenames?
You probably should not, but I have tested that and it seems to work. Please let me know if it does not work (my email address is below).
3. I can't see the difference. Both the Test1 and Test2 links above opens up the File Download window.
You need to set the browser to automatically open up the mp3 in a program (for example Media Player). In Firefox, when the File Download window appears, set the browser to open the file with Media Player. Make sure the "Do this automatically" checkbox is checked. Under Internet Explorer, if there is a checkbox that says "Always ask before opening this type of file", make sure it is NOT checked. Then try again.
4. Must my website be able to run php scripts
Yes, to run this script. Most websites support PHP. However, if your website only runs other scripting languages (eg ASP), you can write your own script using this as a guide. It is a really really simple script. Or you can search the web for other scripts that does this.
5. How did it happen?
If you are using Firefox, the first time you click on an mp3 file, a window pops up, asking you whether to save the file, or to open it with a program on your computer. There is also a checkbox that says "Do this automatically for files like this from now on". If you select that checkbox, the browser does not ask you again. Ever. A similar thing happens on Internet Explorer, and most other browsers.
If you (or someone else using that computer) have previously clicked that checkbox in the past, you may find that when you click on the mp3 file, it does not give you the option to save it. A program (or browser plugin) you install may sometimes do this automatically. Like, for instance, QuickTime.
6. How do I force my browser to ask me whether to save the file?
On Firefox 2.0, click on Tools > Options > Content > File Types > Manage. Then search for the action and click the button labelled "Remove Action". Other versions of Firefox may have slightly different menu structures.
Webmasters can use this script instead. :-)
7. Any other questions
Please email me at getmp3_author@wh38.nonce.info.nospam Note: remove the ".nospam" at the end of that email address.
Last updated: 5/15/2008 7:04:12 AM GMT