PHP Velho Oeste 2024

Большие объекты (LOB)

Иногда для работы приложения необходимо хранить "большие" порции данных в базе. Обычно, под большим понимают объем данных "около 4 килобайт или больше", хотя некоторые базы данных могут спокойно обрабатывать до 32 килобайт, прежде чем размер данных становится "большим". Большие объекты могут быть текстовыми или двоичными. PDO позволяет работать с такими объектами путём установки типа данных PDO::PARAM_LOB в методах PDOStatement::bindParam() или PDOStatement::bindColumn(). PDO::PARAM_LOB сообщает PDO, что нужно пометить эти данные, как поток. И соответственно работать с такими объектами можно, используя API потоков PHP.

Пример #1 Вывод изображения, хранящегося в базе данных

В этом примере переменной $lob задают в соответствие большой объект LOB, а затем отсылают её в браузер с помощью функции fpassthru(). Так как LOB представляется в виде потока, с ним могут работать такие функции, как fgets(), fread() и stream_get_contents().

<?php
$db
= new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
$stmt = $db->prepare("select contenttype, imagedata from images where id=?");
$stmt->execute(array($_GET['id']));
$stmt->bindColumn(1, $type, PDO::PARAM_STR, 256);
$stmt->bindColumn(2, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);

header("Content-Type: $type");
fpassthru($lob);
?>

Пример #2 Вставка изображения в базу данных

В этом примере будет открывается файл с изображением, его файловый указатель передаётся PDO, который в свою очередь вставляет изображение в базу в виде LOB. PDO извлечёт содержимое файла и поместит его в базу наиболее эффективным способом.

<?php
$db
= new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
$stmt = $db->prepare("insert into images (id, contenttype, imagedata) values (?, ?, ?)");
$id = get_new_id(); // какая-то функция для выделения нового ID

// предположим, что мы находимся на странице загрузки файлов на удалённый сервер

$fp = fopen($_FILES['file']['tmp_name'], 'rb');

$stmt->bindParam(1, $id);
$stmt->bindParam(2, $_FILES['file']['type']);
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);

$db->beginTransaction();
$stmt->execute();
$db->commit();
?>

Пример #3 Вставка изображения в базу данных: Oracle

В случае с базами Oracle требуется несколько иной синтаксис для извлечения содержимого файла и помещения в базу. Также необходимо выполнять вставку в рамках транзакции, иначе вставленный LOB будет зафиксирован в базе с нулевой длиной, так как если не обозначить границы транзакции, изменения будут фиксироваться после каждого выполненного запроса.

<?php
$db
= new PDO('oci:', 'scott', 'tiger');
$stmt = $db->prepare("insert into images (id, contenttype, imagedata) " .
"VALUES (?, ?, EMPTY_BLOB()) RETURNING imagedata INTO ?");
$id = get_new_id(); // какая-то функция для выделения ID

// предположим, что мы находимся на странице загрузки файлов на удалённый сервер

$fp = fopen($_FILES['file']['tmp_name'], 'rb');

$stmt->bindParam(1, $id);
$stmt->bindParam(2, $_FILES['file']['type']);
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);

$db->beginTransaction();
$stmt->execute();
$db->commit();
?>

add a note add a note

User Contributed Notes 7 notes

up
11
diogoko at gmail dot com
15 years ago
PDOStatement's methods bindParam and bindValue also work with strings, as in:

<?php
  $data
= file_get_contents($filename);
 
$stmt->bindValue(1, $data, PDO::PARAM_LOB);
 
//...
?>

This was the only way I could make it work with PostgreSQL.
up
10
http://matts.org/
14 years ago
A big gotcha exists for Oracle users.

You have to save CLOB objects using PDO::PARAM_STR, not PDO::PARAM_LOB.

But you MUST send the 4th argument, usually strlen($subject) or you get a LONG error.
up
11
Jeremy Cook
14 years ago
There seems to be a bug that affects example 1 above. PDO::PARAM_LOB when used with pdo::bindColumn() is supposed to return a stream but it returns a string. Passing this string to fpassthru() then triggers an error with the message 'supplied argument is not a valid stream resource'. This has been reported in bug #40913. The work around is to do the following:

<?php
$stmt
= $db->prepare("select contenttype, imagedata from images where id=?");
$stmt->execute(array($_GET['id']));
$stmt->bindColumn(1, $type, PDO::PARAM_STR, 256);
$stmt->bindColumn(2, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);

header("Content-Type: $type");
echo(
$lob);
?>

Since the browser is expecting an image after the call to header() writing the string representation of the binary output with echo() has the same affect as calling fpassthru().
up
4
ben dot leiting at gmail dot com
7 years ago
For selecting data out of Postgres, the data type of the column in the table determined if the parameter bound with PARAM_LOB returned a string or returned a resource.

<?php

// create table log ( data text ) ;
$geth = $dbh->prepare('select data from log ');
$geth->execute();
$geth->bindColumn(1, $dataString, PDO::PARAM_LOB);
$geth->fetch(PDO::FETCH_BOUND);

echo (
$dataString); // $dataString is a string

// create table log ( data bytea ) ;
$geth = $dbh->prepare('select data from log');
$geth->execute();
$geth->bindColumn(1, $dataFH, PDO::PARAM_LOB);
$geth->fetch(PDO::FETCH_BOUND);

fpassthru($dataFH); // $dataFH is a resource
up
2
knl at bitflop dot com
15 years ago
I spend a lot of time trying to get this to work, but no matter what I did PDO corrupted my data.

I finally discovered that I had been using:

$pdo->exec('SET CHARACTER SET utf8');

in the TRY part of my connection script.

This off course doesn't work when you feed binary input to PDO using the parameter lob.
up
0
phpcoder at gmail dot com
5 years ago
The DBMSs that are listed above have these (default) limits on the maximum size of a char string. The maximum is given in bytes so the number of characters storable can be smaller if a multibyte encoding is used.

CUBRID:    16kB
SQL Server: 2GB
Firebird:  32kB
IBM Db2:   32kB
Informix:  32kB
MySQL:     16kB
Oracle:     2kB
PostgreSQL: 1GB
SQLite:     1 billion bytes
4D: Unknown, but LOBs are limited to 2GB.
up
-12
xorinox at gmx dot ch
13 years ago
I find it easier to use stream_get_contens to fetch the data of a lob using the file handle.

<?php
$stmt
= $pdo->con->prepare( 'select * from filetable' );
$stmt->execute();
$res = $stmt->fetchAll( PDO::FETCH_ASSOC );

for(
$i=0; $i<count($res); $i++ ){
 
$filename = "C:/tmp/".$res[$i]['FILE_ID'].'.xml';
 
$content = stream_get_contents( $res[$i]['DATA_FILE'] );
 
file_put_contents( $filename, $content );
}
?>
To Top