Jump to content

How to download a pdf and mp3 file stored in mysql


Jaswinder
Go to solution Solved by Jaswinder,

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;
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 properly get uploaded into database and songs folder too.

Database name - music
table name - mp3
fields - sno and songs

Edited by fenway
code tags
Link to comment
Share on other sites

Wrong using mysql_num_rows function. 

 

Take a look - http://php.net/manual/en/function.mysql-num-rows.php

 

Change:

 

EDIT: Don't call every time mysql_connection in every php script, just use the include php function for good design.

$n=mysql_num_rows;

to

$n=mysql_num_rows(Sql);
Edited by jazzman1
Link to comment
Share on other sites

jazzman1

 

Thanx buddy.... now just 1 problem left...

 

a download dialog box is coming.. but file size is showing just 0 KB ..  but the song size is 4 MB .....

 

I am not able to give proper path,, i think

Your inserting is wrong. You are trying to insert the file name as a blob data.

 

Take a look at this post - http://www.sitepoint.com/forums/showthread.php?693871-Problem-using-PHP-to-pull-binary-files-from-a-BLOB-field-in-MYSQL

Link to comment
Share on other sites

I knew i am sending just filename into database not blob... and storing the original file into a folder named as Songs in my xampp folder..

 

the file is getting there properly....

 

while downloading i am not able to give path to that folder

 

header("Content-Disposition: attachment; filename='<? echo $s[songs]; ?>'");   .. problem lies here... either i have to remove from here echo and use opendir & readdir commands ... or something better,, if u can suggest

Link to comment
Share on other sites

 

I knew i am sending just filename into database not blob... and storing the original file into a folder named as.......

 

Sorry, my misreading.

 

1. You don't have to loop the files in download.php, in case that you want to display only one file.

 

2. Check the correct path to the it should be something like - songs/name_of_the_file

 

This is my suggestion:

 

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) {
    
$s=mysql_fetch_assoc($sql);

// path to the file
$file = 'songs'.'/'.$s['songs'].'.mp3';

header('Content-Description: File Transfer');
header("Content-type: audio/mpeg");
header('Content-Disposition: attachment; filename='.  basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

}
Link to comment
Share on other sites

gud try.... but following errors are coming
 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 15

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 20

Warning: filesize() [function.filesize]: stat failed for songs/.mp3 in C:\xampp\htdocs\upload\download.php on line 21

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 21

Warning: readfile(songs/.mp3) [function.readfile]: failed to open stream: No such file or directory in C:\xampp\htdocs\upload\download.php on line 22
 
 
 
i also tried putting concatenate after header like this
 
header('Content-Description: File Transfer');
header.("Content-type: audio/mpeg");
header.('Content-Disposition: attachment; filename='. basename($file));
header.('Expires: 0');
header.('Cache-Control: must-revalidate');
header.('Pragma: public');
header.('Content-Length: ' . filesize($file));
readfile.($file);
 
it reduces header loaction problem except once...
 
Path - my mp3 files are in C:\xampp\htdocs\upload\songs\filename.mp3
 
and my php files are in
 
Path - C:\xampp\htdocs\upload
 
so i also  tried the file path
 
$file = 'upload'.'/'.'songs'.'/'.$s['songs'].'.mp3';
 
still no results... dont know giving correct path or not...
 
Any clue??
 
i came across another link.. may be it will help .. take a look at this - http://www.finalwebsites.com/forums/topic/php-file-download

Link to comment
Share on other sites

still no results... dont know giving correct path or not...

 

No, the path is fine. You get nothing from $s=mysql_fetch_assoc($sql);

 

Put this code below to the script immediately after $s=mysql_fetch_assoc($sql); and give me the result back.

 

 

if($n>0) {
$s=mysql_fetch_assoc($sql);

 // put this
echo '<pre>'.print_r($s, true).'</pre>'; exit;

// path to the file
$file = 'songs'.'/'.$s['songs'].'.mp3';

..............
Edited by jazzman1
Link to comment
Share on other sites

According to this log you got an empty value from database.

 

Can you explain why?

 

 

Warning: filesize() [function.filesize]: stat failed for songs/.mp3 in C:\xampp\htdocs\upload\download.php on line 21

 

1. Be conscious that download.php and the directory called songs lay on(share) one and the same main directory with name /upload

 

2. For Warning: Cannot modify header information - headers already sent by - click

 

3. When you fixed everything, the path must be:

 

 

// the path to the file

$file = 'songs'.'/'.$s['songs'];

 

 

Link to comment
Share on other sites

filesize may be empty... because its just getting filename from database not file

 

and i changed the path.. still no result

 

Well, it's pretty straightforward there is no file with this name in /songs directory :)

 

 

header problem... i cant change  position of header statement

 

Did you read the link which I sent you above?

Link to comment
Share on other sites

yes i read that link... I try to reorder the header position, but not getting any result...  and i dont understand the Output buffering...

 

O man... you are right... i just deleted files from /songs there yesterday. Now i uploaded again

 

now the  output is

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\upload\download.php:1) in C:\xampp\htdocs\upload\download.php on line 18,19,20,21,22,23,24

 

 and this


ID3vTALBwww.primemusic.ruCOMMengwww.primemusic.ruTYER2011TCONPrime MusicTENCwww.primemusic.ruTCOP*Òîëüêî äëÿ îçíàêîìèòåëüíîãî ïðîñëóøèâàíèÿTRCK1TIT2E - Midnight CallerTCOMTPE1'Chase & Status feat. Clare MaguireÿûdXing!«HŒº "$'),/1468;>@CFHJLOPSVX[^`cegjmpsvy|ƒ…ˆ‹'"–˜šž¡£¥¨ª¬¯±³µ¸»¼¿ÃÅÈËÎÑÔ×ÚÞáãçêìïóõøûüþÿMLAME3.97 º.S4 $MàHŒºâÏÖ1ÿûdði ¤ 4€ "ÃAh´z+Ã@1?wóÜ€"es>J1î]ÁéçÐy`UÉ1ÛòèOÞ@á$?c»ñ0j.—É"ÿûd"ði ¤ 4€³éQ3úD€ä(—Ó'Ëé—?ä¡¥7—tÝ™HÛÿ›¡A¦ÆLÍ™$d´V‡ÿ÷AÿdÓtgwA¶ÿÿÿúÿûdDði ¤ 4€–ûAËÅÓÿÿ÷B'n]crñÕ@A,›{þ­"'ì]ÜΉüãV"nì>(>*Š'h¨r**!i'œ="{'‡Î>]Å9NÍ—<3E2öÐœ—g²ç•ú®tE_ÿÿÿÿÿý›®©/ôµPÚ‡W2!. ÿÿû`d7ãgxIé&ú@ p¥#-¦$Ù \%"ÿÿÿÿÿÿÿÿÿÿÿÿ_ôkAµK÷×ï¥×[JYÑ3‡A¨ëk¿'6Ôp,uùò{úx•ÁçzÇ…zµæmQö£8"6ÒŠ˜Å3)]Œl€œ«Û £]¢Kþ l-ô£ºy"Òú7È Ne×Y2iË&†["N`"†@ÎBB}/‡¼1œ$úUA‹T¸Öpä%éHŇƒÿÿÿÿÿÿÿÿÿÿÿÿ÷¬ÃêÔî'òD&UbWt×I,­ DË¥"å¬2¥Ò¥–áéÀUêe…Qä¨VCPŠ¯8ÿû€d€õ'UJéà`€Pñ+£$Á`T€e@±VBùeÓœ2-»"÷OgÚH.^rVKÅd¤ÕJžuîdôœñ&"rÁ(~+ÂòåÈfKÄVäëN!-.e§Ÿqˈ±ôª6rš? ‹2cHJMjÇ™š¦.3zQåÝ\´NÇ?®Af"ŽsëÙ Ý·"×!-úäÒÓ舛ÿÿÿÿÿÿÿÿÿÿÿÿmB­eú}M†ØEÂ.'vÚûe'"J;‰dÎLia™Á‰QÖ ÛK8™¤HÉáLRZê#¤¨'n_'©Î9Â0•à; »_ÇbI}J˘:ÒõÂ}Ê^6¦Ñ'†CÈb‹—$æ1‡JdÔy$·dòÕI4&4D° …‚ŠH"*‹UèG÷GöÞa#ÖÇÿÿÿÿÿÿÿÿÿÿÿÿÿÑ·mcbTš5FT†Vkl•´AŸümŒu×ÝGeËjtëÈMÿû€dôlOËy‰xÀ `éÙ+¦$WèN€e@!XÊ%OŒ¡ƒâÄùÆÈ ä$'z"ŒÝ&OÎ-9(¢t‰§ Ù$GFÓŒ Å º'30Šõ¡ƒ1…Âpü`cAXa DXŒ€­'x:‡WéœD ·@A ìoÈyó4ˆµLo>ÿÿÿÿÿÿÿÿÿÿÿÿÿÿõ¾õ$cdã'Im¸ÛHƒ'Œ·X§Á{½ Û?y·×ÂÖA|Ú§}ˆ&f>œiD ¤ÔgÊæ"eA¶#¬¹ìrCÏÄ*0<" bJHŸÅ(µ¯Ñ«NIt([˜ÇO$"Òåa‹Ovµg4ã)l›¹ùÓ:Ú©Óç–®·Qn¯¦"ç<"jç–b7+É­9ßÒ¦Õ¿Ûæ¡ùw «/ÿÿÿÿÿÿÿÿÿÿÿÿÿñè±7ª4ESXwYf•Ä"Æ ÷û³ÎŠZl*¹1S› mTK7¬«Ææÿû€dô,GËy(7` @¡)†3`g%ÓPa È'"Ÿ`–¶É½ @¹kªƒgŒ#E"xÐ h¾@.......................................

 

 

i think its showing blob data

Link to comment
Share on other sites

  • Solution

Finally its over.. here's the code

 

<?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)
{
$s=mysql_fetch_assoc($sql);
$file ='songs'.'/'.$s['songs'];
 header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: audio/mpeg");
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
    header("Content-Transfer-Encoding: binary");
      header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile( $file );
}
?>
 

its working like charm

 

Thanx 

jazzman1

.. thanx alot .. i am not able to get this without your help... :happy-04:  :happy-04:

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.