直接上代码:

$content = file_get_contents('avatar.jpg');
$content = base64_encode($content);
$id = 3;
$blobLen = strlen($content);

$conn = new SQLite3('test.db');
$conn->exec("INSERT INTO img (`id`, `name`, `file`,`created_at`) VALUES (". $id .", 'test01', zeroblob(". $blobLen ."), 123)");

$stream = $conn->openBlob('img', 'file', $id, 'main', SQLITE3_OPEN_READWRITE);
fwrite($stream,  $content);
fclose($stream);

$data = $conn->querySingle("SELECT `file` FROM img WHERE id = " . $id);
file_put_contents("a.jpg", base64_decode($data));
$conn->close();

另一种方法:

$db = new SQLite3('mysqlitedb.db');

//获取文件2进制流
$filename = "https://www.jb51.net/logo.gif";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
//创建数据表
$db->exec('CREATE TABLE person (idnum TEXT,name TEXT,photo BLOB)');

$stmt = $db->prepare("INSERT INTO person VALUES ('41042119720101001X', '张三',?)");
$stmt->bindValue(1, $contents, SQLITE3_BLOB);
$stmt->execute();
<?php
$pdo = new SQLite3('mysqlitedb.db');
$results = $pdo->query('select * from person');
while ($row = $results->fetchArray()) {
ob_start();
header("Content-Type: image/jpg");
echo $row['photo'] ;
ob_end_flush();
}

标签: php, sqlite