pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to send bytea data straight to browser (as in pg_loreadall)?
3+ messages / 2 participants
[nested] [flat]

* How to send bytea data straight to browser (as in pg_loreadall)?
@ 2002-04-01 18:31  Frank Joerdens <frank@joerdens.de>
  0 siblings, 2 replies; 3+ messages in thread

From: Frank Joerdens @ 2002-04-01 18:31 UTC (permalink / raw)
  To: pgsql-php; +Cc: adam@archi-me-des.de; linuxkurs@fas-art.com

I don't seem to be able to get my head 'round as to how to do this. It
works OK if I do something like

---------------------- begin snippet ----------------------
$query = "SELECT * FROM bytea_test;";

$conn = pg_pconnect("host=192.168.100.70 dbname=bytea_test");
$result = pg_exec($conn, $query);

$i = 0;
while (@pg_result($result, $i, id)) {

    $fp = fopen('/usr/local/httpd/htdocs/testfile'.$i.'.gif', 'w');
    fwrite($fp, stripcslashes(pg_result($result, $i, image)));
    echo "<img src=\"";
    echo 'testfile'.$i.'.gif';
    echo "\">";
    $i++;

}
---------------------- end snippet ----------------------

which means that I create a file for each image pulled from the database
and then put this file path (relative to http root) into an img tag.

That's rather stupid though, because it's unnecessary, slow, and bound
to give concurrency trouble.

I've played with fgets, fwrite, fputs, fpassthrough, trying to make a
file pointer on stdout with fopen . . . it appears that I don't
understand some crucial point about how this works. How do you present
an image to the browser without actually creating a file for it? The
built in function pg_loreadall appears to do exactly that. I'd need to
replicate that functionality for bytea.

Regards, Frank



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to send bytea data straight to browser (as in pg_loreadall)?
@ 2002-04-01 19:09  Frank Joerdens <frank@joerdens.de>
  parent: Frank Joerdens <frank@joerdens.de>
  1 sibling, 0 replies; 3+ messages in thread

From: Frank Joerdens @ 2002-04-01 19:09 UTC (permalink / raw)
  To: Frank Joerdens <frank@joerdens.de>; +Cc: pgsql-php; adam@archi-me-des.de; linuxkurs@fas-art.com

On Mon, Apr 01, 2002 at 08:31:01PM +0200, Frank Joerdens wrote:
[ . . . ]
> understand some crucial point about how this works. How do you present
> an image to the browser without actually creating a file for it? The
> built in function pg_loreadall appears to do exactly that. I'd need to
> replicate that functionality for bytea.

An addition: Actually, just echoing the stripcslashed string to the
browser works just as with pg_loreadall if I put the script that does
the echoing into a teensy little html page as in

<img src="show_image.php">

where show_image.php is just a script that does

echo stripcslashes(pg_result($result, 2, image));

(after doing the sql stuff, that is).

Which means that I know how to do it without a tmp file. However, I
still don't understand the mechanism and I still don't know how to do it
in a single script. It appears that you have to provide a file path for
the <img src . . . tag. Can you do that *within* a script without
creating a tmp file somehow?

Regards, Frank



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to send bytea data straight to browser (as in pg_loreadall)?
@ 2002-04-01 21:22  Joe Conway <mail@joeconway.com>
  parent: Frank Joerdens <frank@joerdens.de>
  1 sibling, 0 replies; 3+ messages in thread

From: Joe Conway @ 2002-04-01 21:22 UTC (permalink / raw)
  To: Frank Joerdens <frank@joerdens.de>; +Cc: pgsql-php; adam@archi-me-des.de; linuxkurs@fas-art.com

Frank Joerdens wrote:
> I've played with fgets, fwrite, fputs, fpassthrough, trying to make a
> file pointer on stdout with fopen . . . it appears that I don't
> understand some crucial point about how this works. How do you present
> an image to the browser without actually creating a file for it? The
> built in function pg_loreadall appears to do exactly that. I'd need to
> replicate that functionality for bytea.
> 
> Regards, Frank

I've always approached this a bit differently, but in any case, did you 
try to use "php://stdout"? If not see: 
http://www.php.net/manual/en/function.fopen.php


I usually create one php file to generate the image output, and a second 
one which calls the first. E.g. (untested)

---------------------- begin showimgage.php ----------------------
<?PHP

$conn = pg_pconnect("host=192.168.100.70 dbname=bytea_test");
$query = "SELECT img FROM bytea_t where id=" . $_GET["imgid"];
$result = pg_exec($conn, $query);

$image = stripcslashes(pg_result($result, 0, 0));
// send the image
header("content-type: image/gif");
echo $image;

?>
---------------------- showimgage.php ----------------------

The second file would then have a line like:

echo "<img src='showimage.php?imgid=1' border=0>";


Hope this helps,

Joe






^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2002-04-01 21:22 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-04-01 18:31 How to send bytea data straight to browser (as in pg_loreadall)? Frank Joerdens <frank@joerdens.de>
2002-04-01 19:09 ` Frank Joerdens <frank@joerdens.de>
2002-04-01 21:22 ` Joe Conway <mail@joeconway.com>

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