Page 1 of 1

Help - force "save as" link

Posted: Sat Nov 26, 2011 4:09 pm
by synchaoz
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.

Re: Help - force "save as" link

Posted: Sat Nov 26, 2011 10:34 pm
by thinsoldier
You can use str_replace to change spaces to underscores in the downloaded filename.

To reference a file in another folder you need to tell php the full path from the root of the file system the machine is running on.

example on my machine
/users/applications/xamp/htdocs/indevelopment/demosite.com/media/audio/album4/you_keep_saying.mp3

on a windows server it would be like

c:/xampp/htdocs/indevelopment/demosite.com/media/audio/album4/you_keep_saying.mp3

you can echo __file__ in a file in the mp3 folder to find out the full path to that directory

Re: Help - force "save as" link

Posted: Sun Nov 27, 2011 9:00 am
by synchaoz
How do I use str_replace? I literally know nothing about php or coding at all. Like, zero.

Also concerning the full path; I use a rented website server at http://www.one.com. I don't know what the root directory looks like on that.

No idea what you mean about echo __file__

Re: Help - force "save as" link

Posted: Sun Nov 27, 2011 10:06 am
by Celauran
synchaoz wrote:How do I use str_replace?
str_replace()
synchaoz wrote:No idea what you mean about echo __file__

Code: Select all

echo __FILE__;

Re: Help - force "save as" link

Posted: Sun Nov 27, 2011 11:36 am
by synchaoz
Celauran wrote:
synchaoz wrote:How do I use str_replace?
str_replace()
synchaoz wrote:No idea what you mean about echo __file__

Code: Select all

echo __FILE__;
Right. That's all just gibberish to me. I don't know how to code php. The script I am using, and posted in the first post here, is one i just copy/pasted from another website. I have no clue how to expand on it in any way or form.

I was simply hoping that there might be some extra line of code or something I could add to it, that would allow it download file names with spaces intact. Is php at all even capable of that, or is it a waste of time trying to achieve that?

Re: Help - force "save as" link

Posted: Sun Nov 27, 2011 12:20 pm
by mikeashfield
See here:
http://kb.mozillazine.org/Filenames_wit ... n_download
By the look of it, all you need to do is give the filename in double quotes!