PHP PDO 如何读取MSSQL Blob图片

折腾记录
1086 0

前言>从odbc链接方式转到PDO之后 其他代码都好改,但是遇到改读取图片这里遇到问题了 .

本来odbc里这个命令就能解决的东西...

$pic= odbc_result($temp,"pdata");

在pdo 里这样却不行

$sql = "SELECT photo FROM Medlemmer WHERE id=?";
$st = $con->prepare($sql);
$st->execute(array(17));
$st->bindColumn('photo', $photo, PDO::PARAM_LOB);
$st->fetch(PDO::FETCH_BOUND);
odbc_longreadlen($st, 131072);        
odbc_binmode($st,ODBC_BINMODE_CONVERT);                            
ob_clean();
header('Content-Type: image/*');
if ($rd = $st->fetch(PDO::FETCH_BOUND)) {
echo $rd['photo'];
ob_end_flush();
$con = null;

来自国外友人的解答:

PHP和Access ODBC驱动程序从来都不是最好的朋友,显然PDO-ODBC和accessodbc驱动程序仍然是这样。这里的两条皱纹
BLOB作为ASCII字符串返回,表示图像数据的十六进制值(例如“424D7AC000…”),并且
该字符串每255个字符包含一个伪空字符。我仔细查看了浏览器的返回输入,确实是十六进制,在控制台能看到一个· 这样的红色符号

成功运行的代码:

<?php
$dbName = $_SERVER["DOCUMENT_ROOT"]."\\test.mdb";
$con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");
$sql = "SELECT Photo FROM Clients WHERE id=?";
$st = $con->prepare($sql);
$st->execute(array(1));
$st->bindColumn(1, $photoChars, PDO::PARAM_LOB);
$st->fetch(PDO::FETCH_BOUND);

// $photoChars is a long string of hex, e.g., '424D7A...'

// PDO+Access_ODBC apparently injects a NULL every 255 characters, 
//     so remove them first
$photoChars = str_replace("\0", "", $photoChars);

// create array of character pairs (e.g.: '42', '4D', '7A', ...)
$photoArray = str_split($photoChars, 2);

// convert to numeric values
for ($i = 0; $i < sizeof($photoArray); $i++) {
    $photoArray[$i] = hexdec($photoArray[$i]);
}

// pack into binary string
//     ref: http://stackoverflow.com/a/5473057/2144390
$photoData = call_user_func_array("pack", array_merge(array("C*"), $photoArray));

header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_PNG));
header('Content-Disposition: attachment; filename="untitled.bmp"');
echo $photoData;
参考:https://stackoverflow.com/questions/22325904/php-pdo-ms-access-how-to-read-blob-images
https://blog.fxb.cc/63.html
最后更新 2021-12-27
评论 ( 0 )
问:叁 + 叁 = ?
OωO
隐私评论