Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-26.11.1 | composer require openswoole/core:26.11.1
<?php OpenSwoole\Coroutine\PostgreSQL->openLOB(int $oid, string $mode = "rb"): resource|false
The OID of the large object.
Open mode. The default rb opens the object read only, a mode containing w or + (for example wb or r+b) opens it for reading and writing.
Returns a PHP stream resource for the large object, or false on failure, check the error property.
Open an existing large object and return it as a PHP stream. Use the normal stream functions such as fread(), fwrite(), fseek(), stream_get_contents() and fclose() on it.
Large object operations must run inside a transaction. Streams that are still open are closed when the transaction ends (after COMMIT or ROLLBACK).
A large object stream can be passed as a parameter to PostgreSQLStatement->execute(), its OID is sent as the parameter value.
<?php
use OpenSwoole\Coroutine\PostgreSQL;
co::run(function()
{
$pg = new PostgreSQL();
$pg->connect("host=127.0.0.1 port=5432 dbname=test user=postgres password=***");
$pg->query('BEGIN');
$stmt = $pg->query('SELECT data FROM files WHERE id = 1');
$oid = $stmt->fetchAssoc()['data'];
$lob = $pg->openLOB($oid);
var_dump(stream_get_contents($lob));
fclose($lob);
$pg->query('COMMIT');
});