Contents / Previous / Next


Working with Large Objects (Blobs)

A large object (lo = "blob") is referenced by an object-ID "oid" in the DB.

All functions processing large objects take a valid database connection (opened by pg_connect()) called "resource" as parameter.
Do not close the database connection before closing the large object resource.

To use the large object interface, it is necessary to enclose it within a transaction block.


Functions that work direcly on DB (using $dbh):

int pg_lo_create ( resource connection):
Creates a Large Object and returns the oid of the large object or FALSE if an error occurred.

bool pg_lo_unlink ( resource connection, int oid):
Deletes a large object and returns TRUE on success or FALSE on failure.

int pg_lo_import ( [resource connection, string pathname]):
Imports a large object from file.
The pathname argument specifies the pathname of the file to be imported as a large object.
It returns the oid of the just created large object or FALSE if an error occurred.
Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed.

bool pg_lo_export ( int oid, string pathname [, resource connection]):
The oid argument specifies oid of the large object to export and the pathname argument specifies the pathname of the file.
It returns FALSE if an error occurred, TRUE otherwise.


Functions that work on a PHP lo resource:
In PHP a large object is represented by a resource for the large object, returned by pg_lo_open().

resource pg_lo_open ( resource connection, int oid, string mode):
Opens a Large Object and returns large object resource.
oid must be a valid object-ID.
mode can be either "r", "w", or "rw".
It returns FALSE if there is an error.

bool pg_lo_close ( resource large_object):
Closes a Large Object.

string pg_lo_read ( resource large_object, int len):
Reads a large object. It reads at most len bytes from a large object and returns it as a string or FALSE if there is an error.

int pg_lo_write ( resource large_object, string data):
Writes to a large object from a variable data and returns the number of bytes actually written, or FALSE in the case of an error.

int pg_lo_read_all ( resource large_object):
Reads a large object and passes it straight through to the browser after sending all pending headers.
Mainly intended for sending binary data like images or sound. It returns number of bytes read or FALSE, if an error occurred.

bool pg_lo_seek ( resource large_object, int offset [, int whence]):
Seeks the read/write position indicator of a large object, whence is PGSQL_SEEK_SET, PGSQL_SEEK_CUR or PGSQL_SEEK_END.

int pg_lo_tell ( resource large_object):
Returns current position of the read/write position indicator of a large object (offset from the beginning of the large object).