Load Image from File to Store in ByteA Field

Load Image from File to Store in ByteA Field

am 02.02.2004 05:55:27 von middink

This is a multi-part message in MIME format.

------=_NextPart_000_0037_01C3E98B.D4C9B260
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

How to Load Image from File to Store in ByteA Field?

------=_NextPart_000_0037_01C3E98B.D4C9B260
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable








How to Load Image from File to Store in By=
teA=20
Field?

 


------=_NextPart_000_0037_01C3E98B.D4C9B260--

Re: Load Image from File to Store in ByteA Field

am 02.02.2004 20:12:08 von Cornelia Boenigk

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

Re: Load Image from File to Store in ByteA Field

am 03.02.2004 00:49:04 von pntil

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
>
>
>
>



---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Re: Load Image from File to Store in ByteA Field

am 03.02.2004 02:09:04 von Cornelia Boenigk

hi Paul

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

You are 100% right.
My application had to run with PHP below 4.2 where pg_escape_bytea()
was not yet defined. And it works also with higher PHP versions ;-)

Conni



---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Re: Load Image from File to Store in ByteA Field

am 03.02.2004 10:19:32 von middink

How big file size do I stored in ByteA Field.



----- Original Message -----
From: "Paul & Natalie T"
To:
Sent: Tuesday, February 03, 2004 7:49 AM
Subject: Re: [PHP] Load Image from File to Store in ByteA Field


> 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
> >
> >
> >
> >
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Re: Load Image from File to Store in ByteA Field

am 03.02.2004 17:21:16 von Scott Marlowe

Up to about 1 gig (theoretically 2 gig max but encoding might take up some
space.)

On Tue, 3 Feb 2004, Muhyiddin A.M Hayat wrote:

> How big file size do I stored in ByteA Field.
>


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org