Subject RE: [firebird-support] Blob Compression
Author Nigel Weeks
> We need to automatically compress and decompress large text blobs as
> they are written to and read from the database. I believe this can be
> done with a filter but I do not know how to write or impliment it. If
> anyone has some sample code, a link, or knows where there might be a
> good tutorial it would be greatly appreciated.

Filters might be a little more involved, but it's easy
if you do it at your middle layer, ie, in PHP.

Here's a complete walkthrough example:

#!/usr/local/bin/php -e

/* -------- Schema is as follows ----------
create database "localhost:/raid/db/blob.fdb";
SQL> create table tbl_blah (
CON> );


$conn = ibase_connect("localhost:/raid/db/blob.fdb","sysdba","masterkey");

$string = "This isn't a large string of characters to compress";
$gzstring = gzcompress($string);

$insql = "INSERT INTO tbl_blah (blob_field) values (?)";

if(!$inrec = ibase_query($insql,$gzstring)){
echo "The Insert failed for some reason...";

// Ok, there's some compressed info in the field - lets pull it out, and
// if it's compressed, automatically uncompress it.

$outsql = "select * from tbl_blah";
$outrec = ibase_query($outsql);
while($outobj = ibase_fetch_object($outrec,IBASE_FETCH_BLOBS)){
echo "This could be compressed. Let's try it\n";
if(!$text = &gzuncompress($outobj->BLOB_FIELD)){
echo "Decompression failed - must be plain text.\nValue is
} else {
echo "Decompressed: '".$text."'\n";
} // End of fetching objects from recordset