pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul & Natalie T <pntil@shentel.net>
To: pgsql-php@postgresql.org
Subject: Re: Load Image from File to Store in ByteA Field
Date: Mon, 02 Feb 2004 18:49:04 -0500
Message-ID: <401EE1F0.2030106@shentel.net> (raw)
In-Reply-To: <00c101c3e9c0$79373640$0201a8c0@zwerg2000>
References: <003c01c3e948$ee2bc970$4f00a8c0@MIDDINKS>
	<00c101c3e9c0$79373640$0201a8c0@zwerg2000>

Any modern version of PHP which has pg_ specific functions can do it 
like this:

    $string = pg_escape_bytea(file_get_contents($filename));
    pg_query("INSERT INTO TABLE pictures (picture_bytea) VALUES 
('$string');");

I prefer to use pg_escape() rather than code my own escape.

Paul

>Hi
>
>  
>
>>How to Load Image from File to Store in ByteA Field?
>>    
>>
>This is the way I store images with PHP. The column bild has the
>datatype bytea.
>
>function esc_bytea($imagedata) {
> $finde = array(chr(92), chr(0), chr(39));
> $ersetze = array('\\\134', '\\\000', '\\\047');
> $esc = str_replace($finde[0], $ersetze[0], $imagedata);
> $esc = str_replace($finde[1], $ersetze[1], $esc);
> $esc = str_replace($finde[2], $ersetze[2], $esc);
> return $esc;
>}
>
>     $fp = fopen($imagefile,"r");
>     $contents = fread($fp, filesize($imagefile));
>     fclose($fp);
>     $esc_daten = esc_bytea($contents);
>
>     $sql = "INSERT INTO byteatest (bild, name, size, typ, htmlstr) ";
>     $sql .= "values ('$esc_daten', '$name', $size, '$typ',
>'$htmlstr');";
>     $res = @pg_exec($sql) or die ("Fehler bei der
>Datenbankabfrage.");
>
>Regards
>Conni
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 8: explain analyze is your friend
>
>
>  
>





view thread (6+ messages)  latest in thread

Message-ID: <401EE1F0.2030106@shentel.net>
Permalink:  ../401EE1F0.2030106@shentel.net/
Also on:    postgresql.org/message-id/401EE1F0.2030106@shentel.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: pntil@shentel.net
  Subject: Re: Load Image from File to Store in ByteA Field
  In-Reply-To: <401EE1F0.2030106@shentel.net>
  References: <003c01c3e948$ee2bc970$4f00a8c0@MIDDINKS> <00c101c3e9c0$79373640$0201a8c0@zwerg2000> <401EE1F0.2030106@shentel.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox