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() 函式會讀取一個大型物件,並在發送所有待處理的標頭後直接將其傳遞到瀏覽器。主要用於傳送二進制數據,例如影像或聲音。

要使用大型物件介面,必須將其包含在一個事務區塊中。

注意:

此函式以前稱為 pg_loreadall()

參數

lob

一個 PgSql\Lob 實例,由 pg_lo_open() 返回。

回傳值

讀取的位元組數。

更新日誌

版本 說明
8.1.0 lob 參數現在需要一個 PgSql\Lob 實例;先前需要的是 資源

範例

範例 #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 則註解

2
robert dot bernier5 at sympatico dot ca
20 年前
// 記住,大型物件必須從事務內取得
pg_query ($dbconn, "begin");

// 在此範例中,「假設」壓縮檔案的大型物件資源編號為「17899」

$lo_oid = 17899;

$handle_lo = pg_lo_open($dbconn,$lo_oid,"r") or die("<h1>錯誤.. 無法取得控制代碼</h1>");

// 在開始二進位下載之前要傳送到瀏覽器的標頭
header('Accept-Ranges: bytes');
header('Content-Length: 32029974'); // 這是壓縮檔案的大小
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>錯誤,無法讀取大型物件。</h1>");

// 提交資料事務
pg_query ($dbconn, "commit");
0
fabar2 at libero dot it
13 年前
請注意,如果您省略「length」參數,它將讀取一個 8192 位元組的物件,而不管其實際大小。如果您想使用此函式,請考慮在讀取物件之前將物件大小儲存在某處(通常是其表格中的欄位)。或者,使用 pg_lo_readall 函式。
To Top