HELP! I Need to create a mp3 download button!

  • djjuanito
  • Newbie
  • Newbie
  • djjuanito
  • Posts: 5

Post 3+ Months Ago

Hey Guys, I'm a webMaster (in training) by day and a Music Producer by night and I need a little help if somebody can be so kind. I'm building a demo page to send to radio stations across the country of my music, I pretty much have it all done except the DOWNLOAD Button, I can't seem to find the script for that anywhere. Can some one help me out please?

Thanks,

DJ Juanito


http://www.zombeat.com
http://www.zombeat.com/juanitos_demos.htm
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • musik
  • Legend
  • Super Moderator
  • User avatar
  • Posts: 6893
  • Loc: up a tree

Post 3+ Months Ago

Hi and welcome to Ozzu.


Paste this code below into notepad and save as mp3.php - where you see

$file = '/home/mp3/'.$mp3;

change the red bit to be the filename path of where your mp3 files are kept. Save this in your main directory online.

Code: [ Select ]
<?php

$mp3 = $_GET['mp3'];
$file = '/home/mp3/'.$mp3;

if(ini_get('zlib.output_compression')) {
 ini_set('zlib.output_compression', 'Off');
}

if ( file_exists($file) ) {
 header("Pragma: public");
 header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Cache-Control: private",false);
 header("Content-Type: application/mp3");
 header('Content-Disposition: attachment; filename="'.$mp3.'"');
 header("Content-Transfer-Encoding: binary");
 header("Content-Length: ".@filesize($file));
 set_time_limit(0);
 @readfile($file) OR die("<html><body OnLoad=\"javascript: alert('Unable to read file!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
 exit;
} else {
 die("<html><body OnLoad=\"javascript: alert('File not found!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
}

?>
  1. <?php
  2. $mp3 = $_GET['mp3'];
  3. $file = '/home/mp3/'.$mp3;
  4. if(ini_get('zlib.output_compression')) {
  5.  ini_set('zlib.output_compression', 'Off');
  6. }
  7. if ( file_exists($file) ) {
  8.  header("Pragma: public");
  9.  header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
  10.  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  11.  header("Cache-Control: private",false);
  12.  header("Content-Type: application/mp3");
  13.  header('Content-Disposition: attachment; filename="'.$mp3.'"');
  14.  header("Content-Transfer-Encoding: binary");
  15.  header("Content-Length: ".@filesize($file));
  16.  set_time_limit(0);
  17.  @readfile($file) OR die("<html><body OnLoad=\"javascript: alert('Unable to read file!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
  18.  exit;
  19. } else {
  20.  die("<html><body OnLoad=\"javascript: alert('File not found!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
  21. }
  22. ?>



Then in your webpage where you want the download links to be you write your link code like this, where you see the
FileName.mp3
insert your filename. Where you see the
images/graphic.gif
insert your graphic image for your button you want them to press to download the song file.

Code: [ Select ]
<a href="mp3.php?mp3=FileName.mp3"><img src="images/graphic.gif" border ="0" alt="Alt Tag Goes Here"></a>



What you will end up with is a graphic button, when pressed will bring up a dialog box which asks them what they want to do with the file (Open with or Save to disc).

Hope that helps and good luck with your music! ;)
  • djjuanito
  • Newbie
  • Newbie
  • djjuanito
  • Posts: 5

Post 3+ Months Ago

Thanks for your help Musik!

This is the 1st error message I received...

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Unexpected '<' encountered
<?php

Total ActionScript Errors: 1 Reported Errors: 1

...I already have buttons created in my .fla file, I just need to get that code right.

The file name path I was testing with is...
http://www.zombeat.com/radio_demos/wishingonastar.mp3

Let me know if you need more info from me so you can help me resolve this.
I really appreciate your help.

Thanks,

djjuanito


PS. You can check my demo page at http://www.zombeat.com/juanitos_demos.htm ...
  • musik
  • Legend
  • Super Moderator
  • User avatar
  • Posts: 6893
  • Loc: up a tree

Post 3+ Months Ago

Doh! I don't know much about flash I'm afraid, but the link you posted makes the mp3 file come up and ask me what I want to do with it (play or save). So looks like your nearly there.

Just have a play around with it I'm sure you will figure it out or someone with some flash knowledge will come along and help :D

If you get stuck you have my permission to cross post a new topic in the Flash area and ask for some advice.

By the way that script works with any type of file, pdf etc, you just need to edit the code to tell it what kind of file, something handy to keep in your design tips archive.
  • djjuanito
  • Newbie
  • Newbie
  • djjuanito
  • Posts: 5

Post 3+ Months Ago

Thanks Musik.
  • sunspot
  • Born
  • Born
  • sunspot
  • Posts: 2

Post 3+ Months Ago

hello,

I tried to use this code to create a mp3 download button and I found that when I click on the button, it wants to download the php file. What might I be doing wrong
  • musik
  • Legend
  • Super Moderator
  • User avatar
  • Posts: 6893
  • Loc: up a tree

Post 3+ Months Ago

Hi sunspot,

I had a look at your code and the only place I can think you have the code wrong is where you put the path to your directory.

Where you see this line
$file = '/mp3/'.$mp3;

You'll need to put the full path to your hosting directory EG:
$file = '/home/username/public_html/mp3/'.$mp3;

You're nearly there, see when you press the link http://www.markrivera.com/mp3/95-3.mp3 it gives you the box which asks what you want to do with it.

Give that a go and see if works :D


Code: [ Select ]
<?php

$mp3 = $_GET['mp3'];

$file = '/mp3/'.$mp3;

if(ini_get('zlib.output_compression')) {

ini_set('zlib.output_compression', 'Off');

}

if ( file_exists($file) ) {

header("Pragma: public");

header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: private",false);

header("Content-Type: application/mp3");

header('Content-Disposition: attachment; filename="'.$mp3.'"');

header("Content-Transfer-Encoding: binary");

header("Content-Length: ".@filesize($file));

set_time_limit(0);

@readfile($file) OR die("<html><body OnLoad=\"javascript: alert('Unable to read file!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");

exit;

} else {

die("<html><body OnLoad=\"javascript: alert('File not found!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");

}

?>
  1. <?php
  2. $mp3 = $_GET['mp3'];
  3. $file = '/mp3/'.$mp3;
  4. if(ini_get('zlib.output_compression')) {
  5. ini_set('zlib.output_compression', 'Off');
  6. }
  7. if ( file_exists($file) ) {
  8. header("Pragma: public");
  9. header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
  10. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  11. header("Cache-Control: private",false);
  12. header("Content-Type: application/mp3");
  13. header('Content-Disposition: attachment; filename="'.$mp3.'"');
  14. header("Content-Transfer-Encoding: binary");
  15. header("Content-Length: ".@filesize($file));
  16. set_time_limit(0);
  17. @readfile($file) OR die("<html><body OnLoad=\"javascript: alert('Unable to read file!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
  18. exit;
  19. } else {
  20. die("<html><body OnLoad=\"javascript: alert('File not found!');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
  21. }
  22. ?>
  • sunspot
  • Born
  • Born
  • sunspot
  • Posts: 2

Post 3+ Months Ago

I hate to be such a pain, but I am confused about: '/home/username/public_html/mp3/'

/mp3/ is the folder that have the mp3 file in on the root directory, but the rest has me stumped. When I FTP I have access to SITE NAME, HOST NAME, USER ID, and PASSWORD. Would any of this apply to the
'/home/username/public_html/mp3/', and if so what would go where. Sorry for the lack of knowledge, still learning.

Thanks
  • OneWay
  • Born
  • Born
  • OneWay
  • Posts: 1

Post 3+ Months Ago

musik wrote:

Paste this code below into notepad and save as mp3.php - where you see

$file = '/home/mp3/'.$mp3;

change the red bit to be the filename path of where your mp3 files are kept. Save this in your main directory online.

Code: [ Select ]
<?php

$mp3 = $_GET['mp3'];
$file = '/home/mp3/'.$mp3;

if(ini_get('zlib.output_compression')) {
 ini_set('zlib.output_compression', 'Off');
}

if ( file_exists($file) ) {
 header("Pragma: public");
 header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Cache-Control: private",false);
 header("Content-Type: application/mp3");
 header('Content-Disposition: attachment; filename="'.$mp3.'"');
 header("Content-Transfer-Encoding: binary");
 header("Content-Length: ".@filesize($file));
 set_time_limit(0);
 @readfile($file) OR die("<html><body OnLoad="javascript: alert('Unable to read file!');history.back();" bgcolor="#F0F0F0"></body></html>");
 exit;
} else {
 die("<html><body OnLoad="javascript: alert('File not found!');history.back();" bgcolor="#F0F0F0"></body></html>");
}

?>
  1. <?php
  2. $mp3 = $_GET['mp3'];
  3. $file = '/home/mp3/'.$mp3;
  4. if(ini_get('zlib.output_compression')) {
  5.  ini_set('zlib.output_compression', 'Off');
  6. }
  7. if ( file_exists($file) ) {
  8.  header("Pragma: public");
  9.  header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
  10.  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  11.  header("Cache-Control: private",false);
  12.  header("Content-Type: application/mp3");
  13.  header('Content-Disposition: attachment; filename="'.$mp3.'"');
  14.  header("Content-Transfer-Encoding: binary");
  15.  header("Content-Length: ".@filesize($file));
  16.  set_time_limit(0);
  17.  @readfile($file) OR die("<html><body OnLoad="javascript: alert('Unable to read file!');history.back();" bgcolor="#F0F0F0"></body></html>");
  18.  exit;
  19. } else {
  20.  die("<html><body OnLoad="javascript: alert('File not found!');history.back();" bgcolor="#F0F0F0"></body></html>");
  21. }
  22. ?>



Then in your webpage where you want the download links to be you write your link code like this, where you see the
FileName.mp3
insert your filename. Where you see the
images/graphic.gif
insert your graphic image for your button you want them to press to download the song file.

Code: [ Select ]
<a href="mp3.php?mp3=FileName.mp3"><img src="images/graphic.gif" border ="0" alt="Alt Tag Goes Here"></a>



What you will end up with is a graphic button, when pressed will bring up a dialog box which asks them what they want to do with the file (Open with or Save to disc).

Hope that helps and good luck with your music! ;)





Help, the script is fallin' and I can't get it up!

I copied into a text file and named it mp3.php

I put it in my main directory (as well as my root and another one just to be sure). I pasted the other short snippet of code into the page that i wanted it to show up on (with the changes) and i get, "can't find file".

I know the path is correct. Can't figure it out.

I would certainly appreicate your help thanks!

Mark
  • musik
  • Legend
  • Super Moderator
  • User avatar
  • Posts: 6893
  • Loc: up a tree

Post 3+ Months Ago

Can you paste the code you are using.. sometimes the path might seem correct, but it's not :D
  • sohaibsheikh
  • Born
  • Born
  • sohaibsheikh
  • Posts: 1

Post 3+ Months Ago

sorry to interfare but muzik ur code is extremely helpful.
can you please explain how I can put my directory address if I have following weblink to my directory

http://sohaibsheikh.makesgirlscrazy.com/mp3/

my files are stored in "mp3" directory as shown.
  • musik
  • Legend
  • Super Moderator
  • User avatar
  • Posts: 6893
  • Loc: up a tree

Post 3+ Months Ago

Hi sohaibsheikh,

Welcome to Ozzu :D

Without knowing the way your hosting is set up its difficult to say. The best thing to go is log in via FTP and follow the path which leads to the mp3 directory.

This article might help you. Otherwise contact your hosting provider for advice. They will be able to tell you straight away.
  • leetel_ting
  • Born
  • Born
  • leetel_ting
  • Posts: 2

Post 3+ Months Ago

Hi musik, are you still around? I realize this thread is old but I was hoping someone could give me a tip. I found your script and am trying to implement it and am running into an issue. When the download button is clicked I get pages of data, including the album title of my mp3 but no downloading is happening.

It seems to be set up right in that the script is finding the file at least (this is an improvement over the blank white page I was getting and the "cannot find file"). Any idea why the pages of data would appear?

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: spork and 65 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.