Page 1 of 1

BLOB storing/retrieving problem

PostPosted: 22. February 2012 21:54
by oracle_nic
After a long, long, long period of testing on almost windows platforms, the problem of storing retrieving a blob file such as jpeg, word, pdf in a BLOB field of a table in the database.
The problem is that in the relative record is stored or retrieved from it only partialy, resulting an error concerning the integrity of the contents. Since this problem has to be resolved ASAP, any suggestion would be of great importance.
The scripts are:

(1) Selecting a file for upload ad_01.php
-----------------------------------------------
<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>

(2) Insertin a file into DB ad_02.php
------------------------------------------
<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>

(3) Display the contents of the table add_03.php
---------------------------------------------------------
<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>

(4) Open/Save the contents of the BLOB field add_04.php
--------------------------------------------------------------------
<?php

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

if(isset($_GET['id']))
{
$id =$_GET['id'];
}

$query = "SELECT name, type, size, data FROM myfiles WHERE id = $id";
$result = mysql_query($query);
if($row = mysql_fetch_array($result))
{
$data = $row['data'];
$type = $row['type'];}
header("Content-type: $type");
echo $data;
?>
==================================================================

Re: BLOB storing/retrieving problem

PostPosted: 22. February 2012 22:33
by JonB
It might be fairly useful to know what version of XAMPP you are using and what Windows OS we are talking about. Please add the information to your user profile.

viewtopic.php?f=13&t=48626

Thanks
8)





ycaf

Re: BLOB storing/retrieving problem

PostPosted: 23. February 2012 08:07
by oracle_nic
Sorry,
totaly dummy me

version 1.7.7 VC9

running on XP 32 bit

thx for replying

Re: BLOB storing/retrieving problem

PostPosted: 23. February 2012 20:20
by JonB
then it would probably also useful to know what errors are being propagated.

As in "specifics"

are there errors in the Apache log, phperrors, and/or MySQL. What is your reporting level in PHP?? Any errors to browser???

is it ALL blob objects, or just one of a certain type OR size??

You may want to review this earlier XAMPP topic

viewtopic.php?f=16&t=49387&p=190542&hilit=blob#p190542


:?: