Jump to content

How to download an mp3 file stored in folder using PHP and Mysql


Jaswinder
 Share

Recommended Posts

Note - my uploaded file goes into a folder name- songs i used the following code to upload :- In player.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head> <body><form action="upload.php" method="post" enctype="multipart/form-data"><input type="file" name="i"/><input type="submit" value="Upload" /></form></body></html> In upload.php <? $i=$_FILES['i']['name'];copy($_FILES['i']['tmp_name'],"songs"."/".$i);$cn=mysql_connect("localhost","root","");$db=mysql_select_db("music",$cn);$a="insert into mp3 values('','$i')";$sql=mysql_query($a) or die(mysql_error);if($sql>=0){ echo "Successfully Entered";}else{ echo "failed";} ?><a href="show.php">show files</a> For downloading In show.php <?$cn=mysql_connect("localhost","root","");$db=mysql_select_db("music",$cn);$a="select * from mp3";$sql=mysql_query($a) or die(mysql_error);$n=mysql_num_rows($sql);if($n>0){while($f=mysql_fetch_array($sql)){?><table align="center" border="2" width="200" height="100"><td><a href="download.php?sno=<? echo $f['sno'];?>">Download</a></td></tr></table> <?} }else{echo 'No data';}?> In download.php<?php$download=$_GET['sno'];$cn=mysql_connect("localhost","root","");$db=mysql_select_db("music",$cn);$a="select songs from mp3 where sno='$download'";$sql=mysql_query($a) or die(mysql_error);$n=mysql_num_rows($sql);if($n>0){while($s=mysql_fetch_array($sql)){header("Content-type:audio/mpeg");header("Content-Disposition: attachment; filename='<? echo $s[songs]; ?>'"); i think here is the problem,.. not able to download files}} ?> Database name - musictable name - mp3fields - sno and songs Path to files - C:\xampp\htdocs\upload Path to songs folder where uploaded mp3 files go - C:\xampp\htdocs\upload\songs

Link to comment
Share on other sites

So I never seen anyone use copy to move the uploaded file to it's destination,if you followed any tutorial they would have used this function, very curious did youactually upload anything with copy to the destination?http://www.php.net/m...loaded-file.php Also you need to escape your user submitted data before having it in the query.http://www.php.net/m...cape-string.php You should look at mysqli since mysql is deprecated and will disaper from the near future.http://www.php.net/mysqli

Edited by sircatsquid
Link to comment
Share on other sites

You did not mention what is hppening instead, what does it show, errors. If you have outputs already made it should not send headers second time and show some error. does it show anything? make sure you have error enabled to show all level of error

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...