I am trying to make a website which support mp3 downloading. I don't have any idea with this. I just tried like uploading image , in that I can store mp3 files into database and from there I can move that file into specified folder. I don't know here I am using correct method or not. Now I want to make downloadable mp3 file by clicking a link.Please guide me to complete this.
Thanx in advance.

Below I am pasting my code...

<?php
include ("config.php");
?>
<html>
<head>
<body>
<form name="song" action="" method="post" enctype="multipart/form-data">
Upload MP3:<input type="file" name="mp3" id="mp3"><br/>
<input type="submit" name="btnsubmit" value="Submit">
</form>
<?php
$select=mysql_query("select * from mp3_file")or die(mysql_error());
$result=mysql_result($select,0,'song');
if(isset($_POST["btnsubmit"]))
{
$file_name =$_FILES['mp3']['name'];
$temp = $_FILES['mp3']['tmp_name'];
$insertion=mysql_query("insert into mp3_file values('','$file_name')")or die(mysql_error());
if($insertion)
{
move_uploaded_file($temp,$file_name);
}
}
?>
</body>
</head>
</html>

greetings mr. Sandeepek,

I would like to answer your question in a weel step by step manner.

things you need to consider:

Do not store files directly to the database because it will be heavy for the database. store the files to a folder where the file path along with the file name is stored in the database and not the file itself.

$_FILES is an array function that's why there is square brackets.

enumerating the commonly used $_Files array indexes.
1.) File name
2.) File type
3.) File weight
4.) File protection <--chmod

Now for your question of storing the file in the database is getting the file name. the file path is set by default to your deserved location in this manner it will be something like

$path="myLocation\".$FILES['name'];

in the output it will be myLocation\file.mp3

this will be the one to be used in anchor tag. so you can able to download it with out time trafficking

Hi masterjiraya,
I have tried like what you said .Mp3 file is storing in seperate folder and I wrote the path in anchor tag. Now it's coming like when I am clicking that downlaod link the song is playing in browser it's self.It's not downloading . What was the issue here?...Please help me....!

I'm sorry for the late reply because of our different time zone.
Ok that's great. the problem right now is the server configuration. if it is localhost, try this

find the root.htaccess on your localhost folder add this

`<Files *.mp3>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>

<Files *.ogg>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>

<Files *.avi>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>`

AddType application/octet-stream mp3
AddType application/octet-stream ogg
AddType application/octet-stream avi

if it doesn't have try to force it on php level to undertstand these code snippet I gave you. read for the topic in PHP website called header("Content-type: application/octet-stream"); <-- this is the key you need to force download any files just like the code below:

<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$path\"\n");
$fp=fopen("$path", "r");
fpassthru($fp);
?>

your foundation of octet streaminmg has now triggered in $_FILES

now I would like to tell you how was octet-stream programming in php level do for you.

"Content-type: application/octet-stream" is the one you called for root.htacces to access all octet-streams for streaming and download media files.
"Content-disposition:attachment; filename=\"$path\"\n" is the one used for calling octet-stream files to assign it only as attachment type by the syntax of Content-disposition:attachment;

if you want only for streaming, then it will be Content-disposition:stream; otherwise do not type the 3rd line I gave you.

fopen() is needed as always if you have Content-disposition:attachement; it will be default as zip file or compressed file.
fpassthru() is needed once you have fopen() and Content-disposition:attachement;

Hi Masterjiraya,

Sorry for the late replay., I have done like what you said.., now I am able to download the mp3 file.
But the problem is while opening the browser or if I am refreshing the browswer it is asking to downlaod the mp3 file. I need like if I am clicking the link that time only it should ask to downlaod.
Please help me to solve this.
Thanx in advnce..,

<?php
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="samsunggal_v7h8lenf.mp3"');
$fp=fopen("samsunggal_v7h8lenf.mp3", "r");
fpassthru($fp);
include ("config.php");
?>
<html>
<head>
<body>
<form name="song" action="" method="post" enctype="multipart/form-data">
Upload MP3:<input type="file" name="mp3" id="mp3"><br/>
<input type="submit" name="btnsubmit" value="Submit">
</form>
<?php
$select=mysql_query("select * from mp3_file")or die(mysql_error());
$result=mysql_result($select,0,'song');
if(isset($_POST["btnsubmit"]))
{
$file_name =$_FILES['mp3']['name'];
$temp = $_FILES['mp3']['tmp_name'];
$insertion=mysql_query("insert into mp3_file values('','$file_name')")or die(mysql_error());
if($insertion)
{
move_uploaded_file($temp,$file_name);
}
}
$path=samsunggal_v7h8lenf.'.'.mp3;//"myLocation/".$FILES['name'];
echo '<a href="samsunggal_v7h8lenf.mp3">Downlaod</a>';
?>
<table>
<tr>
<td>
<a href="<?php echo $path; ?>">MP3</a>
<a href="download"><img src="<?php echo $result;?>"></a>
</td>
</tr>
</table>
</body>
</head>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.