note 114885 added to book.sqlite3
| From: | bohwaz | Date: | Mon, 21 Apr 2014 00:47:02 +0000 |
| Subject: | note 114885 added to book.sqlite3 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199304@lists.php.net to get a copy of this message | ||
Another undocumented feature of the PHP SQLite3 object, available since 2009 is openBlob. Basically
it's a function that will return a stream pointer to a blob value in a table. Very very useful
when you are dealing with files stored in a SQLite3 database.
Source code says:
proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname])
Open a blob as a stream which we can read / write to.
But despite that it's not possible to write to the blob, only to read (the write method is
empty in the extension source code: it won't return any error but no change will be recorded).
One example of use:
<?php
$db = new SQLite3('files.sqlite');
$db->exec('CREATE TABLE files (id INTEGER PRIMARY KEY, filename TEXT, content BLOB);');
$statement = $db->prepare('INSERT INTO files (filename, content) VALUES (?, ?);');
$statement->bindValue('filename', 'Archive.zip');
$statement->bindValue('content', file_get_contents('Archive.zip'));
$statement->execute();
$fp = $db->openBlob('files', 'content', $id);
while (!feof($fp))
{
echo fgets($fp);
}
fclose($fp);
?>
You can also seek in the stream. This is pretty useful for saving large files from the database too,
this way you can use stream_copy_to_stream, it will be faster and more memory-efficient than dumping
the file in memory before writing it to the disk.
Please note that openBlob() won't work on VIRTUAL FTS4 compressed tables.
----
Server IP: 195.221.21.36
Probable Submitter: 87.88.42.116
----
Manual Page -- http://php.net/manual/en/book.sqlite3.php
Edit -- https://master.php.net/note/edit/114885
Del: integrated -- https://master.php.net/note/delete/114885/integrated
Del: useless -- https://master.php.net/note/delete/114885/useless
Del: bad code -- https://master.php.net/note/delete/114885/bad+code
Del: spam -- https://master.php.net/note/delete/114885/spam
Del: non-english -- https://master.php.net/note/delete/114885/non-english
Del: in docs -- https://master.php.net/note/delete/114885/in+docs
Del: other reasons-- https://master.php.net/note/delete/114885
Reject -- https://master.php.net/note/reject/114885
Search -- https://master.php.net/manage/user-notes.php