Help - force "save as" link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synchaoz
Forum Newbie
Posts: 3
Joined: Sat Nov 26, 2011 3:55 pm

Help - force "save as" link

Post 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.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Help - force "save as" link

Post 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
Warning: I have no idea what I'm talking about.
synchaoz
Forum Newbie
Posts: 3
Joined: Sat Nov 26, 2011 3:55 pm

Re: Help - force "save as" link

Post 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__
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help - force "save as" link

Post 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__;
synchaoz
Forum Newbie
Posts: 3
Joined: Sat Nov 26, 2011 3:55 pm

Re: Help - force "save as" link

Post 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?
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Help - force "save as" link

Post 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!
Post Reply