blob retrieve from database

Problems with the Windows version of XAMPP, questions, comments, and anything related.

blob retrieve from database

Postby oracle_nic » 19. December 2011 00:44

I have created a database with a table named filenames which consists from 5 columns
Code: Select all
id-> primary key - autoincrement - integer
name -> varchar (30)
type -> varchar (30)
size -> integer
data -> blob
which has to save blob (pics, doc, txt, pdf, e.t.c.) objects
the scripts conerning this effort are:

add_01.php
-------------
Code: Select all
<HTML>
<BODY text="#FFFFFF" bgcolor="#000000" >
<p align="center">
<font face="Verdana">
Nicholas 1 (select file)
</font>
</p>
<hr>
<?
echo "<p align = \"center\">";
   echo "<font face=\"Verdana\">";
echo "<FORM METHOD=\"post\" ACTION=\"add_02.php\" ENCTYPE=\"multipart/form-data\">";
echo "<INPUT TYPE=\"hidden\" NAME=\"MAX_FILE_SIZE\" VALUE=\"2000000\">";
echo "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"upload\">";
echo "<TABLE BORDER=\"1\">";
echo "<TR>";
echo "<TR>";
echo "<TD>File: </TD>";
echo "<TD><INPUT TYPE=\"file\" NAME=\"myfile\"></TD>";
echo "</TR>";
echo "<TR>";
echo "<TD COLSPAN=\"2\"> <p align = \"center\"> <INPUT TYPE=\"submit\" VALUE=\"Upload\"> </p> </TD>";
echo "</TR>";
echo "</TABLE>";
echo "</FORM>";
   echo "</font>";
   echo "</p>";
?>
<hr>
</BODY>
</HTML>

add_02.php
-------------
Code: Select all
<HTML>
<BODY text="#FFFFFF" bgcolor="#000000" >
<p align="center">
<font face="Verdana">
Nicholas 2 (insert uploaded file into database)
</font>
</p>
<hr>
<?php

$name = $_FILES['myfile']['name'];
$type = $_FILES['myfile']['type'];
$size = $_FILES['myfile']['size'];
$temp = $_FILES['myfile']['tmp_name'];

$fp = fopen($temp, 'r');
$data = fread($fp, filesize($temp));
fclose($fp);
$data = addslashes($data);

echo "<font face=\"Verdana\">";
echo "file name: "; echo $name; echo "<br>";
echo "file type: "; echo $type; echo "<br>";
echo "file size: "; echo $size; echo "<br>";
echo "temp location: "; echo $temp  ; echo "<br>";
echo "</font>";
echo "<hr>";

if ($name <> NULL and $type <> NULL and $data <>NULL and $size <>0 and $temp <> NULL)
{

   $db = mysql_connect("localhost", "root", "");
   mysql_select_db("alpha", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
   
   $qry = "insert into myfiles (name, type, size, data) values (\"$name\", \"$type\", \"$size\", \"$data\")";

   mysql_query($qry) or die("Error... Query Failed!"); echo "<br>";
   echo "<p align = \"center\"><font face =\"VERDANA\" color=\"GREEN\">Record Inserted</font><hr></p>";
   echo "<br>";
   //mysql_free_result($result);
   mysql_close($db);
}
else
{
   echo "<p align = \"center\"><font face =\"VERDANA\" color=\"RED\">Unable to Insert Record<hr></font></p>";
   echo "<font color=\"YELLOW\"<hr>$data</font><hr>";
}
?>
</BODY>
</HTML>

add_03.php
--------------
Code: Select all
<HTML>
<BODY text="#FFFFFF" bgcolor="#000000" >
<p align="center">
<font face="Verdana">
Nicholas 3 (show database records)
</font>
</p>
<hr>
<?php

$db = mysql_connect("localhost", "root", "");
mysql_select_db("alpha", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");

echo "<p align = \"center\">";
echo "<font face= \"VERDANA\">";
echo "<table border = \"1\" width = \"100%\">";
echo "<td align=\"center\" bgcolor=\"GREEN\">"; echo "ID"; echo "</td>";
echo "<td align=\"center\" bgcolor=\"GREEN\">"; echo "NAME"; echo "</td>";
echo "<td align=\"center\" bgcolor=\"GREEN\">"; echo "TYPE"; echo "</td>";
echo "<td align=\"center\" bgcolor=\"GREEN\">"; echo "SIZE"; echo "</td>";
echo "<td align=\"center\" bgcolor=\"RED\">";  echo "DOWNLOAD FILE"; echo "</td>";
echo "<td align=\"center\" bgcolor=\"BLUE\">";  echo "UPDATE FILE"; echo "</td>";
echo "<td align=\"center\" bgcolor=\"BLUE\">";  echo "DELETE FILE"; echo "</td>";

$qry = "select id, name, type, size, data from myfiles";
$result = @mysql_query($qry, $db);
while ($row = mysql_fetch_array($result))
{
$id = $row[0]; $name = $row[1]; $type = $row[2]; $size = $row[3];
echo "<tr>";
   echo "<td align=\"right\">"; echo $id; echo "</td>";
   echo "<td>"; echo $name; echo "</td>";
   echo "<td>"; echo $type; echo "</td>";
   echo "<td align=\"right\">"; echo $size; echo "</td>";
   
   echo "<td align=\"center\" bgcolor=\"WHEAT\">";
   echo "<a href = \"add_04.php?id=$id\">";
   echo "<img src=\"osiris.jpg\" />";
   echo "</a>";
   echo "</td>";
//
   echo "<td align=\"center\" bgcolor=\"WHEAT\">";
   echo "<a href = \"add_05.php?id=$id\">";
   echo "<img src=\"pencil.jpg\" />";
   echo "</a>";
   echo "</td>";
//   
   echo "<td align=\"center\" bgcolor=\"WHEAT\">";
   echo "<a href = \"add_06.php?id=$id\">";
   echo "<img src=\"axeman.jpg\" />";
   echo "</a>";
   echo "</td>";

   echo "</tr>";
}
echo "</table>";
echo "</p>";

mysql_free_result($result);
mysql_close($db);
?>

<hr>
<p align="center">

</p>
</BODY>
</HTML>

add_04.php
-------------
Code: Select all
<?php
//display blob
$db = mysql_connect("localhost", "root", "");
mysql_select_db("alpha", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
$id = $_GET['id'];
//$id = $_REQUEST['id'];
$sql = "SELECT id, name, type, size, data FROM myfiles WHERE id = $id";
$result = mysql_query($sql) or die (mysql_error());
list ($id, $name, $type, $size, $data) = mysql_fetch_array($result);

header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
//$data = stripslashes($data);
echo $data;
exit;
?>


As it is obvious script add_01.php calls script add_02.php to upload and store blob files into database and script add_03.php calls add_04.php in order to open/download the blob files. Since now I' ve checked that the blob entries were successfully inserted in the database.
When I choose to open directly or after saving the blob file for all types (img/jpeg, msword/doc or pdf) of files except text i receive the message that the file type is not supported from windows applications in order to present them correctly!
Forgot to say that ms word files are written in Greek characters.
oracle_nic
 
Posts: 8
Joined: 04. August 2011 18:23

Re: blob retrieve from database

Postby Sharley » 19. December 2011 00:49

Nicholas, would you be so kind as to add your XAMPP version and Operating System to your profile as per these instructions:
viewtopic.php?f=16&t=48626
Thanks. :)

I have also added code tags to make the reading of your php code easier for troubleshooting. 8)
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 33 guests