PHP PDO MS Access如何读取Blob图片? [英] PHP PDO MS Access how to read blob images?
问题描述
我在MS ACCESS数据库中具有BLOB的图像.到目前为止,我已经将它们与PHP的odbc acces一起使用,并且工作正常.这是简化程序:
I have images as BLOB's in an MS ACCESS database. I have so far used them with odbc acces from PHP and it works fine. Here comes the simplified program:
code:
<?php
ini_set("odbc.defaultlrl", "5M");
$dbName = $_SERVER["DOCUMENT_ROOT"]."\\..\db\\teknofo.mdb";
$con = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=".$dbName,'','') or die('Ups');
ob_clean();
header('Content-Type: image/*');
$sql = "SELECT photo FROM Medlemmer WHERE Id=17";
$rd = odbc_exec($con, $sql);
if (odbc_fetch_row($rd)) { echo odbc_result($rd,"photo"); }
odbc_close($con);
ob_end_flush();
?>
我正在转换为MySql,但必须对某些timg使用MS Access: 因此,我正在使用PDO编写新代码,但是我无法正确读取数据.
I am in the process of converting to MySql but will have to use MS Access for some timg: Therefor I am making the new code using PDO, but I am not able to read the data correct.
这是新的
<?php
$dbName = $_SERVER["DOCUMENT_ROOT"]."\\..\db\\teknofo.mdb";
$con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");
$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;
?>
最后一个代码可以与MySql(更改的连接字符串)一起使用,但不能与MS Access一起使用.
The last code Works fin with MySql (changed connection string) but not with MS Access.
我已经把网缝了很长时间了,但是找不到解决方法.
I have searced the net for a long time but have not been able to find a solution.
有人可以求助吗?
我可以使用第一个代码,但是我还需要能够处理BLOB的其他用途.
I could use the first code, but I need to be able to handle BLOB's for other purposes as well.
推荐答案
PHP和Access ODBC驱动程序从来都不是最好的朋友,而且显然PDO_ODBC和Access ODBC驱动程序仍然是这种情况.这里的两个皱纹是
PHP and the Access ODBC driver have never been the best of friends, and apparently that continues to be the case with PDO_ODBC and the Access ODBC driver. The two wrinkles here were
-
BLOB作为表示图像数据十六进制值的ASCII字符串返回(例如'424D7AC000 ...'),
The BLOB is returned as an ASCII string representing the hex values of the image data (e.g., '424D7AC000...'), and
该字符串每255个字符包含一个伪造的NULL字符.
That string contains a spurious NULL character every 255 characters.
我设法开始工作的代码是:
The code I managed to get working is:
<?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;
这篇关于PHP PDO MS Access如何读取Blob图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!