使用PDO和stream_get_contents将Oracle BLOB映像作为空资源返回

时间:2019-06-30 20:26:09

标签: php oracle pdo blob

我正在尝试从存储在Oracle XE数据库中的BLOB检索图像。

测试“那里有东西”:

enter image description here

因此,我使用PDO进行了尝试:

<?php
//carrega_foto_oracle.php?funcionario_id=10    

include '..\conexao_oracle.php';

$stmt = $pdo->prepare("select mimetype, binario from foto where funcionario_id=?");
$stmt->execute(array($_REQUEST['funcionario_id']));
$stmt->bindColumn(1, $type, PDO::PARAM_STR);
$stmt->bindColumn(2, $blob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
header("Content-Type: $type");
echo stream_get_contents($blob);

我的浏览器抱怨“我的图片中有一些问题”,所以我试图转储资源(在标头函数之前):

var_dump(stream_get_contents($blob));exit();

结果是一个空字符串:(

string(0) "" 

有什么我想念的吗?

预先感谢

编辑:我的联系人:

<?php
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_CASE => PDO::CASE_LOWER //opcional: campos com nome minúsculo
];

$servidor = "localhost";
$usuario = "php";
$senha = "admin";
$service_name = "XE";
$sid = "XE";
$port = 1521;
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $servidor)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";

$pdo = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $usuario, $senha, $options);

1 个答案:

答案 0 :(得分:0)

我有这个旧的工作样本。但是,您确实应该使用OCI8,而不是PDO_OCI

<?php

try {
    $db = new PDO('oci:dbname=localhost/orclpdb1', 'cj', 'cj');
} 
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}   

$stmt = $db->prepare("drop table cjblob");
$stmt->execute();

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $db->prepare("create table cjblob (id number, data blob)");
$stmt->execute();

function do_insert($db, $id, $data)
{
    $db->beginTransaction(); 
    $stmt = $db->prepare("insert into cjblob (id, data) values (:id, empty_blob()) returning data into :blob");
    $stmt->bindParam(':id', $id);
    $stmt->bindParam(':blob', $blob, PDO::PARAM_LOB);
    $blob = null;
    $stmt->execute();
    fwrite($blob, $data);  
    fclose($blob);  
    $db->commit();
}

do_insert($db, 1, str_pad("Z",  40000, "X"));

// Fetch it back
$stmt = $db->prepare('select data from cjblob where id = ?');
$id = 1;
$stmt->execute(array($id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump(stream_get_contents($row['DATA']));

?>