Received: from joeconway.com (unknown [63.210.180.150]) by postgresql.org (Postfix) with ESMTP id D098B475BB0; Wed, 3 Apr 2002 11:12:33 -0500 (EST) Received: from [206.19.64.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 3.5.6) with ESMTP-TLS id 320445; Wed, 03 Apr 2002 08:07:35 -0800 Message-ID: <3CAB29FD.90606@joeconway.com> Date: Wed, 03 Apr 2002 08:12:45 -0800 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.9) Gecko/20020311 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Jean-Christophe FABRE Cc: pgsql-sql@postgresql.org, pgsql-php@postgresql.org Subject: Re: bytea or large object References: <5.0.0.25.0.20020403164135.00ad0380@ensam.inra.fr> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200204/6 X-Sequence-Number: 1030 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