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

一个 PgSql\Connection 实例。当 connection 未指定时,将使用默认连接。默认连接是 pg_connect()pg_pconnect() 最近建立的连接。

警告

从 PHP 8.1.0 开始,使用默认连接已弃用。

object_id

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

返回值

一个大对象 OID,或在失败时为 false

变更日志

版本 说明
8.1.0 connection 参数现在期望一个 PgSql\Connection 实例;以前,期望的是一个 资源

范例

范例 #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");
?>

添加注释

用户贡献的注释 1 个注释

0
andrea dot galli at acotel dot com
21 年前
<?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