Help - force "save as" link
Posted: Sat Nov 26, 2011 4:09 pm
Hey, I don't know crap about php, at all. I can do basic html and that's it.
I've got some mp3 tracks of my own that I wanna make available for download on my website. But regular a href links doesn't always prompt for download, but instead tries to open them directly in the browser - since I've already got an online mp3 player that streams them live with a play option, I want a dedicated download feature alongside.
So, I've searched around a bit, and the best option I've found so far was a short php script:
<?php
header('Content-disposition: attachment; filename=song.mp3');
header('Content-type: audio/mpeg');
readfile('"song.mp3"');
?>
This method does work, but it has an annoying flaw. It will only download correctly if the file name doesn't contain any spaces. So let's say I have a track called "synchaoz - song.mp3"
The only way I can get the php script to properly download that song, is by renaming it to something like "synchaoz_-_song.mp3"
I've tried inputting different parameters in the songtitle, such as "synchaoz%20-%20song.mp3" but nothing seems to work.
Also another annoying thing is that I can only get it to work if the .php file is located in the same directory as the mp3 file itself, because it apparently doesn't recognize file paths like "audio/songs/synchaoz - song.mp3" etc.
Any idea on how to fix this, or is my only option to remove all spaces in the song titles?
Thanks.
I've got some mp3 tracks of my own that I wanna make available for download on my website. But regular a href links doesn't always prompt for download, but instead tries to open them directly in the browser - since I've already got an online mp3 player that streams them live with a play option, I want a dedicated download feature alongside.
So, I've searched around a bit, and the best option I've found so far was a short php script:
<?php
header('Content-disposition: attachment; filename=song.mp3');
header('Content-type: audio/mpeg');
readfile('"song.mp3"');
?>
This method does work, but it has an annoying flaw. It will only download correctly if the file name doesn't contain any spaces. So let's say I have a track called "synchaoz - song.mp3"
The only way I can get the php script to properly download that song, is by renaming it to something like "synchaoz_-_song.mp3"
I've tried inputting different parameters in the songtitle, such as "synchaoz%20-%20song.mp3" but nothing seems to work.
Also another annoying thing is that I can only get it to work if the .php file is located in the same directory as the mp3 file itself, because it apparently doesn't recognize file paths like "audio/songs/synchaoz - song.mp3" etc.
Any idea on how to fix this, or is my only option to remove all spaces in the song titles?
Thanks.