Mp3

  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

After some Google searching on "mp3 download links" I found a video that referenced a php script written by the user Musik.

website-design-forum/help-need-create-mp3-download-button-t80663.html

The thread is dead so i was hoping someone could help me with this. I found this website last night and this is my first post so I will give a little background. I've had basic html and some graphic design work in high school and around 2 months ago I started watching "TheNewBoston" xhtml, javascript and a good amount of Html5 tutorials. Programming is defiantly new for me, my first website project is for my best friend who is becoming a music producer, and one of the links on my page is the download page where people will be able to download his music. So hopefully with some help i can finish this page.
*My website is not online yet. I work in notepad++ and all my works saved on my computer*

I have the php file saved as mp3.php, swapped out the red text like music said and i copied and pasted the folder path but when i clicked the link it just opened a new page with nothing but the code written in it

Code: [ Select ]
<?php

$mp3 = $_GET['mp3'];
$file = 'C:\Users\Nick\Music\Music'.$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 = 'C:\Users\Nick\Music\Music'.$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. ?>


and link itself

Code: [ Select ]
<a href="mp3.php?mp3=GucciWild.mp3">Stupid Wild</a>
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Hi Ruttah,

The reason the file is showing the code is PHP is a server side code, meaning it has to be compiled by the server software before anything will show. You will need to run your code on a web server, or install one on your own computer for testing. XAMPP is easily installed on your home computer and can be used for local testing. It has a PHP compiler built in, and is quite easy to setup.

XAMPP: Link

Next, the code you are using has a few minor errors that will prevent it from running. Please refer to my corrected code below for a working copy:

Code: [ Select ]
<?php

$mp3 = $_GET['mp3'];
$file = '/your/directory/structure/goes/here/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'>Unable to read file!</body></html>");
exit;
} else {
die("<html><body OnLoad='javascript: alert('File not found!');history.back();' bgcolor='#F0F0F0'>File not found!</body></html>");
}

?>
  1. <?php
  2. $mp3 = $_GET['mp3'];
  3. $file = '/your/directory/structure/goes/here/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'>Unable to read file!</body></html>");
  18. exit;
  19. } else {
  20. die("<html><body OnLoad='javascript: alert('File not found!');history.back();' bgcolor='#F0F0F0'>File not found!</body></html>");
  21. }
  22. ?>



To see a working copy go here: Link
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

Thank you very much Poly. Would Php work if i got a url through my ISP or only if i sign up to one of those Webhosting websites?(I've never really understood the difference)
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

Thank you very much Poly :D . Would Php work if i got a url through my ISP or only if i sign up to one of those Webhosting websites?(I've never really understood the difference)

and also once installed do i have to do anything special to XAMPP to get the php to read correctly?
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

That would depend entirely on your ISP service. Some offer PHP, some do not. Once you have XAMPP installed and running, you will need to place the PHP files in your root directory for XAMPP. If you look at the documentation for this, they will tell you where that is.

Its usually something like this:

C:\XAMPP\www\
or
C:\Program Files\XAMPP\www\

You will need to consult the documentation to find out where it is. Once you find it, drop your files in there, open your browser and point it to your local IP or to 127.0.0.1/filename.php and it will load up.
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

Thank you for your time Poly. Now is the only difference between getting domain from ISP or webhosting is pricing and what languages it understands?

and

If the webhosting or ISP doesn't support either php or javascript are they interchangeable?

Thanks again :)
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

PHP and Javascript are not interchangeable. Virtually every webhost out there supports PHP. Can't remember the last time I heard of one that didn't.
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

Ok sweet deal, thanks for all the info..

I've got all my website stuff where it should be on XAMPP and everything works except "file not found" comes up when i click my download link.

Code: [ Select ]
<?php
$mp3 = $_GET['mp3'];
$file = 'C:\Users\Nick\Music\Website'.$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'>Unable to read file!</body></html>");
exit;
} else {
die("<html><body OnLoad='javascript: alert('File not found!');history.back();' bgcolor='#F0F0F0'>File not found!</body></html>");
}
?>
  1. <?php
  2. $mp3 = $_GET['mp3'];
  3. $file = 'C:\Users\Nick\Music\Website'.$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'>Unable to read file!</body></html>");
  18. exit;
  19. } else {
  20. die("<html><body OnLoad='javascript: alert('File not found!');history.back();' bgcolor='#F0F0F0'>File not found!</body></html>");
  21. }
  22. ?>
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

You need to use the directory structure that the webserver looks at. So instead of using c:\Users\Nick\Music\Website, you will need to use the webservers structure, for example: /xampp/httdocs/wwww/music/. I am not sure what yours is setup as, and I haven't admined a windows based server before, so I'm not that familiar with how the structure on there would be setup. You will most likely need to consult the XAMPP documentation.

Basically whats happening is the server is attempting to look for c:\Users\Nick\Music\Website. However, it can't find it because it can't see outside the root file directory for your web server. So to break it down, its trying to do this C:\xampp\c:\Users\Nick\Music\Website which doesn't exist. So you need to tell it a directory structure it can find/understand.
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

Code: [ Select ]
<?php
$mp3 = $_GET['mp3'];
$file = 'C:\xampp\htdocs\JosueWebsite'.$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'>Unable to read file!</body></html>");
exit;
} else {
die("<html><body OnLoad='javascript: alert('File not found!');history.back();' bgcolor='#F0F0F0'>File not found!</body></html>");
}
?>
  1. <?php
  2. $mp3 = $_GET['mp3'];
  3. $file = 'C:\xampp\htdocs\JosueWebsite'.$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'>Unable to read file!</body></html>");
  18. exit;
  19. } else {
  20. die("<html><body OnLoad='javascript: alert('File not found!');history.back();' bgcolor='#F0F0F0'>File not found!</body></html>");
  21. }
  22. ?>


I'm not sure if thats what you meant but i copied and moved the file into htdocs and file not found still comes up :crazyeyes:

Does it matter that i have your script in a different window, does it need to be mixed in with the html itself?
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

Quote:
<?php
$file = "C:\xampp\htdocs\JosueWebsite\Mp3".$_GET['mp3']."";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=GucciWild.mp3".basename($mp3));
header( "Content-Description: File Transfer");
@readfile($file) OR die("<html><body OnLoad='javascript: alert('Unable to read file!');history.back();' bgcolor='#F0F0F0'>Unable to read file!</body></html>");
exit;

?>


Solved.. Thanks for all the help bud
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Just got back from vacation and saw this now. Glad you got it all figured out!
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Posts: 9

Post 3+ Months Ago

Ha Thanks to you! Now back to javascript to see if i can fine tune my navigation links :)

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 50 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.