php8.5
Home/ Manual/ pdo / pgsql/ Pdo\Pgsql::lobOpen

Pdo\Pgsql::lobOpen

PHP function Edit on GitHub ✎

(PHP 8 >= 8.4.0)

Opens an existing large object stream

Description

Pdo\Pgsql::lobOpen(string $oid, string $mode = "rb"): resource|false

Pdo\Pgsql::lobOpen opens a stream to access the data referenced by oid. All usual filesystem functions, such as fread(), fwrite() or fgets() can be used to manipulate the contents of the stream.

Parameters

oid

A large object identifier.

mode

The access mode. If mode contains w or +, the stream is opened for reading and writing; otherwise it is opened for reading only. A b (binary) flag has no effect. The default "rb" opens the stream for reading.

Return Values

Returns a stream resource on success,Falseforfailure.

Examples

Pdo\Pgsql::lobOpen example

Following on from the Pdo\Pgsql::lobCreate example, this code snippet retrieves the large object from the database and outputs it to the browser.

php
<?php
$db = new Pdo\Pgsql('pgsql:dbname=test host=localhost', $user, $pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$stmt = $db->prepare("SELECT oid FROM BLOBS WHERE ident = ?");
$stmt->execute([$some_id]);
$stmt->bindColumn('oid', $oid, PDO::PARAM_STR);
$stmt->fetch(PDO::FETCH_BOUND);
$stream = $db->lobOpen($oid, 'r');
header("Content-type: application/octet-stream");
fpassthru($stream);
?>

See Also

Source: reference/pdo_pgsql/pdo/pgsql/lobopen.xml · from the official PHP manual (php/doc-en)