Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

How to download a pdf and mp3 file stored in mysql

Codes here !

Moderators: macek, egami, gesf

How to download a pdf and mp3 file stored in mysql

Postby Jaswinder » Wed Mar 27, 2013 7:56 am

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;
if($n>0)
{
while($s=mysql_fetch_array($sql))
{
header("Content-type:audio/mpeg");
header("Content-Disposition: attachment; filename='<? echo $s[songs]; ?>'");
}
}

?>

Note :- I think i am not able to give proper path of file to download... but the file is prperly get uploaded into database and songs folder.

Database name - music
table name - mp3
fields - sno and songs
Jaswinder
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Mar 27, 2013 7:38 am

Re: How to download a pdf and mp3 file stored in mysql

Postby jevin94 » Fri Mar 29, 2013 10:14 am

It will better if you will store any content in folder and store its path only in database.

that will easy to share between 2 user and easy to download

for example visit www.codebook.co.in
jevin94
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Mar 29, 2013 10:02 am

Re: How to download a pdf and mp3 file stored in mysql

Postby Jaswinder » Fri Mar 29, 2013 11:16 pm

i am doing that.. but no result,,,
and the link you provided,,, I am not able to get anything from there...

i prefer if u tell me the proper url to this topic or any directions...
Jaswinder
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Mar 27, 2013 7:38 am

Re: How to download a pdf and mp3 file stored in mysql

Postby johnj » Thu Apr 04, 2013 1:22 am

When you download the content type should change according to what is getting downloaded. It cannot be audio/mpeg "Always".
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 5 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron