Making A Download Button?

Status
Not open for further replies.

kjb1891

New member
Mar 12, 2007
216
1
0
What's the best way to make a button download a file from a URL? Some kind of onclick/javascript code maybe?
 


Don't both of those methods link to the URL and not download from it?

The URL I want the button to download from is a MP3 file.
 
I'm not sure if you can force a download, because a user can decide what he wants to do with different file types in his own browser options. If you want to be certain that something is going to be downloaded and not played in a browser, make a "right click and save" note near the link.
 
Man, I'm confused.

So, how do sites like the ones that are on Clickbank and such give the files to their customers?
 
I can give you some help with this, but you'll have to do a little research also. I've done this with xml files, but not mp3s. There will be some differences to take into account.

Here is some code I used. The file is cached for user defineavble time period. I wrote this a year or so ago so I don't remember exactly how it works, I'm sure others here can help as well.

Hope this helps.

Code:
header("Content-type: text/xml");
header("Content-Disposition: attachment; filename=".basename($file_name));
$maxage = 60 * 60 * 24 * 30; // number of seconds to cache page - last number is days only change this number
header('Last-Modified: '. gmdate ('D, d M Y H:i:s') . ' GMT'); // when page was last modified change to gmdate ('D, d M Y H:i:s',timestamp) to specify date
header('Cache-Control: max-age='.$maxage.', must-revalidate'); // number of seconds before page should be reloaded
$expire=gmdate('D, d M Y H:i:s',time()+$maxage); //Greenwich Mean Time
header('Expires: '.$expire. ' GMT'); // when the cache expires
header('ETag: '.time()); // browser checks this agains info in chached file
print $data;
 
  • Like
Reactions: kjb1891
It would be the content-disposition line that does it for IE. Not sure if it works for firefox though.
 
OK, let me see if I have this straight. I have absolutely no PHP experience at all. All I know is HTML. So, if I sound like an idiot that's because I am :error:


So, I want to save a PHP file that's programmed like this right?:
Code:
<!-- Saved as "audio.php" for this example -->
<?php
header('Content-disposition: attachment; filename=URL/Filename.mp3');
header('Content-type: audio/mp3');
readfile('URL/Filename.mp3');
?>
Then, on my HTML page I have the button that accesses the PHP file like this right?:
Code:
<a href="URL/audio.php" style="text-decoration:none;"><button>Download Now</button></a>

Am I close at all?
 
I think this will work, but I think your type should be audio/mpeg

Also, make sure you take security into account. If your links to the files such as audio.php?mp3=gangsta.mp3 make sure you check the input so that you do not allow people to download your php files or worse.
 
If your links to the files such as audio.php?mp3=gangsta.mp3 make sure you check the input so that you do not allow people to download your php files or worse.

Oh yeah. So it would be:
Code:
<form method=link" action="URL/audio.php">
<input type="button" value="Download Now">
</FORM>

Or I guess this would do the same wouldn't it?:
Code:
<input type="button" value="Download Now" onClick="window.location.href='URL/audio.mp3'">
 
Is audio.php always going to download the same file?

What I was saying is that if you are passing a parameter to audio.php as in the example I gave you to tell audio.php which mp3 to download you need security measures in place. You must validate the parameter passed to make sure the user is not rewriting yor url to request a file you don't want them to have, such as a php file with your database login information.

I can't stress enough how important this is. If your not sure what to do about this do a search for php form validation, cross site scripting, etc.

If your audio.php is always going to send the user one and only one file then you won't need to pass a parameter telling it what file the user wants.

Hope this helps.
 
If your audio.php is always going to send the user one and only one file then you won't need to pass a parameter telling it what file the user wants.

Yeah, there will be only one file. So, that won't be a problem. I've got it working good right now. :D

Thank you so much for your help. :bowdown:
 
Status
Not open for further replies.