Jump to content

Download MP3 with header


cmgmyr

Recommended Posts

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:

[code=php:0]<?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");
}

?> [/code]

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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) ?

[quote author=dark dude link=topic=112942.msg458644#msg458644 date=1161965629]
And btw, slightly offtopic, but good example band + song given thar :P
[/quote] You can never go wrong with that song ;)
Link to comment
Share on other sites

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:[code]$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); [/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.