PHPCon Poland 2024

pg_lo_create

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_lo_create新建大对象

说明

pg_lo_create(PgSql\Connection $connection = ?, mixed $object_id = ?): int
pg_lo_create(mixed $object_id): int

pg_lo_create() 新建大对象并返回大对象的 oid。不支持 PostgreSQL 访问模式 INV_READINV_WRITEINV_ARCHIVE,创建的对象始终以读写方式访问。INV_ARCHIVE 已经从 PostgreSQL(6.3 及以上版本)中移除。

要使用大对象接口,必须将其封装在一个事务块中。

不使用大对象接口(没有访问控制,使用起来很麻烦),试试 PostgreSQL 的 bytea 列类型和 pg_escape_bytea()

注意:

本函数以前的名字为 pg_locreate()

参数

connection

An PgSql\Connection instance. When connection is unspecified, the default connection is used. The default connection is the last connection made by pg_connect() or pg_pconnect().

警告

As of PHP 8.1.0, using the default connection is deprecated.

object_id

如果指定 object_id,该函数将尝试使用此 id 创建大对象,否则服务器将分配一个空闲对象 id。该参数依赖于 PostgreSQL 8.1 中首次出现的功能。

返回值

大对象 OID, 或者在失败时返回 false

更新日志

版本 说明
8.1.0 现在 connection 参数接受 PgSql\Connection 实例,之前接受 resource

示例

示例 #1 pg_lo_create() 示例

<?php
$database
= pg_connect("dbname=jacarta");
pg_query($database, "begin");
$oid = pg_lo_create($database);
echo
"$oid\n";
$handle = pg_lo_open($database, $oid, "w");
echo
"$handle\n";
pg_lo_write($handle, "large object data");
pg_lo_close($handle);
pg_query($database, "commit");
?>

add a note

User Contributed Notes 1 note

up
0
andrea dot galli at acotel dot com
21 years ago
<?php
// --------- OPEN CONN ---

$conn = pg_connect("host='127.0.0.1' dbname='test' user='usertest' password='passtest'");

// --------- OPEN FILE ---

$fp = fopen('logo.gif', "r");
$buffer = fread($fp, filesize('logo.gif'));
fclose($fp);

// --------- CREATE - INSERT OID ---

pg_exec($conn, "begin");

$oid = pg_locreate($conn);

$rs = pg_exec($conn,"INSERT INTO test(tipo, images) VALUES('A1', $oid);");
$handle = pg_loopen ($conn, $oid, "w");

pg_lowrite ($handle, $buffer);
pg_loclose ($handle);

pg_exec($conn, "commit");

// --------- OPEN - INSERT OID ---

$rs = pg_exec($conn, "SELECT images FROM test WHERE tipo = 'A1';");
$row = pg_fetch_row($rs, 0);

pg_exec($conn, "begin");
$loid = pg_loopen($conn, $row[0], "r");

header("Content-type: image/gif");

pg_loreadall($loid);
pg_loclose($loid);

pg_exec ($conn, "commit");

// --------- UNLINK OID ---

pg_exec($conn, "begin");

$loid = $row[0];
pg_lounlink($conn, $loid);

pg_exec ($conn, "commit");

// --------- DELETE OID ---

pg_exec($conn, "DELETE FROM test WHERE tipo = 'A1';");

// --------- CLOSE CONN ---

pg_close();
?>
To Top