PHP Conference Japan 2024

pg_lo_read_all

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

pg_lo_read_all 读取整个大对象并直接发送到浏览器

说明

pg_lo_read_all(PgSql\Lob $lob): int

pg_lo_read_all() 读取大对象并在发送完所有待发的 header 之后将其直接发送给浏览器。主要用于发送图片或声音等二进制数据。

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

注意:

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

参数

lob

通过 pg_lo_open() 返回的 PgSql\Lob 实例。

返回值

读取的字节数。

更新日志

版本 说明
8.1.0 现在 lob 接受 PgSql\Lob 实例,之前接受 resource

示例

示例 #1 pg_lo_read_all() 示例

<?php
header
('Content-type: image/jpeg');
$image_oid = 189762345;
$database = pg_connect("dbname=jacarta");
pg_query($database, "begin");
$handle = pg_lo_open($database, $image_oid, "r");
pg_lo_read_all($handle);
pg_query($database, "commit");
?>

参见

添加备注

用户贡献的备注 2 notes

up
2
robert dot bernier5 at sympatico dot ca
20 years ago
// remember, large objects must be obtained from within a transaction
pg_query ($dbconn, "begin");

// "assume" for this example that the large object resource number of the zipped file is "17899"

$lo_oid = 17899;

$handle_lo = pg_lo_open($dbconn,$lo_oid,"r") or die("<h1>Error.. can't get handle</h1>");

//headers to send to the browser before beginning the binary download
header('Accept-Ranges: bytes');
header('Content-Length: 32029974'); //this is the size of the zipped file
header('Keep-Alive: timeout=15, max=100');
header('Content-type: Application/x-zip');
header('Content-Disposition: attachment; filename="superjob.zip"');

pg_lo_read_all($handle_lo) or
die("<h1>Error, can't read large object.</h1>");

// committing the data transaction
pg_query ($dbconn, "commit");
up
0
fabar2 at libero dot it
13 years ago
Pay attention that if you omit the "length" parameter it will read a 8192 bytes object regardless to its real dimensions. If you want to use this function think to save the object size somewhere (usually a field in its table) before reading the object. Alternatively use the pg_lo_readall function.
To Top