Jump to content


Photo

Download MP3 with header


  • Please log in to reply
5 replies to this topic

#1 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 27 October 2006 - 03:36 PM

Hey everyone,
I would like to have a download script that will take a file name from the databse and rename it, then prompt for download. This is what I have so far:

<?php 
	global $user;
	$maid = $_GET['maid'];
	$cookie_read = explode("|", base64_decode($user));
	$userid = $cookie_read[0];
	
	include "connect.php";
	
	//Find the mp3
	$result = mysql_query("SELECT * FROM mp3_activity WHERE maid = '$maid' AND userid = $userid AND pur = 1") or die('Query failed: ' . mysql_error());
	if (mysql_numrows($result) <= 0){
		echo "You can not download this file.";
	}else{
		$row = mysql_fetch_array($result);
		$mid = stripslashes($row['mid']); 

		//Find the mp3
		$result = mysql_query("SELECT * FROM mp3 WHERE mid = '$mid'") or die('Query failed: ' . mysql_error());
		$row = mysql_fetch_array($result);
		$title = stripslashes($row['title']); 
		$bandid = stripslashes($row['bandid']); 
		$mp3 = stripslashes($row['mp3']); 
		//Find Band name
		$result = mysql_query("SELECT * FROM users WHERE userid = '$bandid'") or die('Query failed: ' . mysql_error());
		$row = mysql_fetch_array($result);
		$name = stripslashes($row['name']); 
		
		//want to re-name the $mp3 file here
	
		header("Content-Type: audio/mpeg");
		header("Content-Disposition: attachment; filename=$mp3"); 
		header("Pragma: no-cache"); 
		header("Expires: 0"); 
	}

?>

all mp3's are stored in the directory "mp3". What I want to have happen is, is when the user goes to download the file it renames the file to Band Name - MP3 title.mp3 (ex. Metallica - Entersandman.mp3). The filename that is in the system was randomly generated so it would be something like rvtzsr7ec25quj7u.mp3

Thanks,
-Chris

#2 dark dude

dark dude
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 27 October 2006 - 04:13 PM

mysql_query("UPDATE mp3 SET {whichever field holds the band name}='{bandname variable here}', {whichever field holds the song name}='{songname variable here}' WHERE userid = '$bandid'");

might work... I dnno, try it and tell us what happens...


And btw, slightly offtopic, but good example band + song given thar :P

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 27 October 2006 - 04:36 PM

"filename" is what the user will see, not the actual file accessed; therefore, pull the band information from the database to update this: Content-Disposition: attachment; filename=file_name_here.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 27 October 2006 - 05:18 PM

ok, so if I make the filename Metallica - Enter Sandman.mp3:
header("Content-Disposition: attachment; filename=Metallica - Enter Sandman.mp3");

How do I get that to download from mp3/$mp3 (mp3/rvtzsr7ec25quj7u.mp3) ?

And btw, slightly offtopic, but good example band + song given thar :P

You can never go wrong with that song ;)

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 27 October 2006 - 06:37 PM

Have a look at this.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 27 October 2006 - 06:47 PM

Thank you for that link, I missed that the first time I went through that page :)

Here is my final header portion of the script:
$fileHandle = 'mp3/'.$mp3;
		$new_name = "$name - $title.mp3";
		header('HTTP/1.1 200 OK');
		header('Date: ' . date("D M j G:i:s T Y"));
		header('Last-Modified: ' . date("D M j G:i:s T Y"));
		header("Content-Type: audio/mp3"); 
		header("Content-Length: " . (string)(filesize($fileHandle)) );
		header("Content-Transfer-Encoding: Binary"); 
		header('Content-Disposition: attachment; filename='.$new_name ); 
		readfile($fileHandle);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users