Re: bytea or large object

From: Joe Conway <mail(at)joeconway(dot)com>
To: Jean-Christophe FABRE <jean-christophe(dot)fabre(at)ensam(dot)inra(dot)fr>
Cc: pgsql-sql(at)postgresql(dot)org, pgsql-php(at)postgresql(dot)org
Subject: Re: bytea or large object
Date: 2002-04-03 16:12:45
Message-ID: 3CAB29FD.90606@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php pgsql-sql

Jean-Christophe FABRE wrote:
>
> Hi,
>
> I would like to store .pdf files in a database, which is accessed through
> a PHP interface. which is the best way to store binary data? -
> bytea? (with the tricky handling of escape chars) - large objects?
>
> thanks
>
> JC
>
> PS: I didn't found any example of scripts using bytea in PHP, does
someone
> has some?

Here's an escape script that was sent to me by Thomas T. Thai.

function escByteA($binData) {
/**
* \134 = 92 = backslash, \000 = 00 = NULL, \047 = 39 = Single Quote
*
* str_replace() replaces the searches array in order. We must
* process the 'backslash' character first. If we process it last,
* it'll replace all the escaped backslashes from the other searches
* that came before.
*/
$search = array(chr(92), chr(0), chr(39));
$replace = array('\\\134', '\\\000', '\\\047');
$binData = str_replace($search, $replace, $binData);
return $binData;
}

There is also a function built in to PHP 4.1.2, available if you're
using PostgreSQL 7.2, called pg_escape_bytea.

Read the pdf file into a string, then use either of these to escape the
string. The you can do a simple SQL insert. To retrieve the original
files, use stripcslashes() to restore the query result string.

Hope this helps,

Joe

In response to

Responses

Browse pgsql-php by date

  From Date Subject
Next Message Michael Adler 2002-04-03 20:58:55 FULL JOIN with 3 or more tables
Previous Message Timothy_maguire 2002-04-03 14:48:06 Re: bytea or large object

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2002-04-03 16:39:31 Re: Primary and foreign key from system tables
Previous Message Jan Oravec 2002-04-03 15:07:02 Primary and foreign key from system tables