Jump to content

Downloading doc,jpg files stored in a DB table


swatisonee

Recommended Posts

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
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.