• No replies
17917 Posts

Pinned topic DB2 UDB, BLOBs and PHP - a deadly combination ?

‏2005-06-13T11:15:05Z |
Hi everyone,

I have a big problem that I've been trying to solve for a week.
I am using DB2 UDB v8.2 and PHP 5.0.4. The goal is to save PDF files in our database using a down- and upload-PHP-interface.

create table pdftab (
a int,
pdf BLOB (2 G) not logged

the PHP script looks like the following.

$con = ODBC_connect(...);

$handle = fopen("test.pdf", "rb");

// $data = fread($handle, filesize($handle));
// this doesnt work either ==> echo $data does not display anything ???????
while (!feof($handle)) {
$data .= fread($handle, 8000);
// elimate special characters like ' \
$data = bin2hex($data);

$sql = "insert into test.pdftab values (11, BLOB('$data'))";

$return = odbc_exec($con, $sql) or die(" Error : " . odbc_error() . odbc_errormsg());
The insert-stmt fails because of the string_constant lenght limitation. The other idea was to update the BLOB incrementally in the while-loop where the Byte-data is read into the $data variable:

"update test.pdftab set pdf = pdf || BLOB('$data') where a = 4";

But concatening requires two expressions and each is a string constant. So the prelimited size gets exceeded once again.
I have tried Other variations of the insert:

.... values(11, $data)
.... values(11, BLOB($data))
.... values(11, "x'".$data."'")
I know that a binary constant is stored as HEX(ASCII(bin_const)) in a BLOB column and is displayed in a format like that: x'ff3c'
Using a parameter marker like:
$sql = "update pdftab set pdf = pdf || BLOB('?') where a = 4";
doesn't help either. With ... BLOB(?) ... it is the same.

Is there another way to insert a BLOB-column without using the BLOB(...) casting function that has predefined (and unchangeable) string-constant-length ???