swatisonee Posted May 29, 2010 Share Posted May 29, 2010 Hi, I used a code available at http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx to upload doc,jpg files to a table on my database. The table looks like this : id name type size content 1 Ras.doc application/msword 27648 [bLOB - 27.0 KB] 4 logo-.jpg image/pjpeg 13412 [bLOB - 13.1 KB] 5 8.pdf application/pdf 22820 [bLOB - 22.3 KB] Now I needed to allow my users to go thru the list and select 1 of the files to download . However, when one clicks on the select button, a lot of junk characters come on the screen esp in the case of the jpg. I just need that when they select a file to download, it should open up a "save as" and they should be able to save it on their desktops. I cannot figure out how to do this part of the download and would be grateful for some help in this regard. <? header("Cache-Control: public"); //session_start(); include ("../logindl.php"); //db info for DB2 ?> <? if(!isset($_SESSION['userid'])){ echo "<center><font face='Calibri' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit;} echo $userid; include ("indl.php"); if($_POST['submit']){ $a = $_POST["choice"]; $userid = $_POST["userid"]; echo $a; echo "<font face=Tahoma size=2 color=#CACA00>"; print(date("l F d, Y")); echo "<p><font color=#CACA00 size=4>Files you can download: "; $queryx = "SELECT name, type, size, content FROM upload WHERE id = '$a'"; echo $queryx; $resultx = mysql_query($queryx) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($resultx); header("Content-Type: application/octet-stream"); header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo $content; exit; ?> <html> <head> <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> <meta name=Generator content="Microsoft Word 11 (filtered)"> <title>Download File</title> </head> <body lang=EN-IN> <form enctype=multipart/form-data action="<?php print $PHP_SELF ?>" method="post"> <input type="hidden" name="userid" value="<? echo $userid ?>"> <?php } else { ?> <form enctype=multipart/form-data action="<? echo $PHP_SELF ?>" method=post> <font face="Tahoma" size="2" color="#0000FF"> <div align="left"> <table> <font face="Tahoma" size="3" color="#CACA00"> <b>Select a file to read.</b><p> <table border="1" style="border-collapse: collapse" bordercolor="#111111" width="100%"> <tr bgcolor="#FFFF99"> <td><font size=2 face=Tahoma color="#339966">Select</b></td> <td><font size=2 face=Tahoma color="#339966">Name</b></td> <td><font size=2 face=Tahoma color="#339966">Type</b></td> <td><font size=2 face=Tahoma color="#339966">Size</b></td> </tr> <? $sql = "SELECT * FROM `upload` "; $result = mysql_query($sql); if ($myrow = mysql_fetch_array($result)) { do { printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> <font size=2 face=Tahoma color=#00FF00><b>%s</b><td> <font size=2 face=Tahoma color=#00FF00><b>%s</b><td> <font size=2 face=Tahoma color=#00FF00><b>%s</b><td> </tr>", $myrow["id"], $myrow["name"], $myrow["type"], $myrow["size"]); } while ($myrow = mysql_fetch_array($result)); } else { echo "No new files at this time."; } ?> <input type="hidden" name="userid" value="<? echo $userid ?>" > </table> <p> <br><input type="submit" name="submit" value="Select"> </p> </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/203258-downloading-docjpg-files-stored-in-a-db-table/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.