4 * PostgreSQL LOB stream
9 * // GET /image.php?image=1234
10 * if (PgLobStream::$loId = (int) $_GET['image']) {
11 * if ($lob = fopen('pglob://dbname=database user=mike', 'r')) {
12 * HttpResponse::setContentType('image/jpeg');
13 * HttpResponse::setStream($lob);
14 * HttpResponse::send();
19 * @copyright Michael Wallner, <mike@iworks.at>
20 * @license BSD, revised
33 function stream_open($path, $mode)
35 $path = trim(parse_url($path, PHP_URL_HOST
));
38 if ($this->dbh
= pg_connect($path)) {
39 if (pg_query($this->dbh
, 'BEGIN')) {
40 if (is_resource($this->loh
= pg_lo_open($this->dbh
, $this->lon
= self
::$loId, $mode))) {
41 pg_lo_seek($this->loh
, 0, PGSQL_SEEK_END
);
42 $this->size
= (int) pg_lo_tell($this->loh
);
43 pg_lo_seek($this->loh
, 0, PGSQL_SEEK_SET
);
52 function stream_read($length)
54 return pg_lo_read($this->loh
, $length);
57 function stream_seek($offset, $whence = PGSQL_SEEK_SET
)
59 return pg_lo_seek($this->loh
, $offset, $whence);
62 function stream_tell()
64 return pg_lo_tell($this->loh
);
69 return pg_lo_tell($this->loh
) >= $this->size
;
72 function stream_flush()
77 function stream_stat()
79 return array('size' => $this->size
, 'ino' => $this->lon
);
82 function stream_write($data)
84 return pg_lo_write($this->loh
, $data);
87 function stream_close()
89 if (pg_lo_close($this->loh
)) {
90 return pg_query($this->dbh
, 'COMMIT');
92 pg_query($this->dbh
, 'ROLLBACK');
98 stream_register_wrapper('pglob', 'PgLobStream');