9

谁能告诉我如何使用 PHP 将图像作为 BLOB 存储在 Oracle 数据库中?

一个工作的例子会很好。谢谢你。

4

3 回答 3

17

$_FILES您首先需要从#global 数组中获取正在上传的图像:

$image = file_get_contents($_FILES['image_field_name']['tmp_name']);

然后在数据库中插入图像试试这个:

$sql = "INSERT INTO table (id, image) VALUES(1, empty_blob()) RETURNING image INTO :image";

$result = oci_parse($connection, $sql);
$blob = oci_new_descriptor($connection, OCI_D_LOB);
oci_bind_by_name($result, ":image", $blob, -1, OCI_B_BLOB);
oci_execute($result, OCI_DEFAULT) or die ("Unable to execute query");

if(!$blob->save($image)) {
    oci_rollback($connection);
}
else {
    oci_commit($connection);
}

oci_free_statement($result);
$blob->free();
于 2012-08-15T13:44:17.587 回答
0

您也可以使用 PDO:

<?php

   include '../conexao_oracle.php';

   $db = $pdo;
   $db->beginTransaction(); // Essential!
   $mimetype = 'image/jpeg';
   $funcionario_id = 10;
   $stmt = $db->prepare(
       "INSERT INTO foto (mimetype, binario, funcionario_id) ".
       "VALUES (:mimetype, EMPTY_BLOB(), :funcionario_id) ".
       "RETURNING binario INTO :binario");
   $stmt->bindParam(':mimetype', $mimetype);
   $stmt->bindParam(':binario', $blob, PDO::PARAM_LOB);
   $stmt->bindParam(':funcionario_id', $funcionario_id);
   $blob = fopen('fotos/10.jpg', 'rb');
   $stmt->execute();
   $pdo->commit();
于 2019-06-30T18:29:55.797 回答
0

谢谢 DRiFTy,我在您的示例上构建了我的小型 JSON 查询。让我们澄清一下,您可以使用这两个示例保存大图像。

<?php
// Database parameters
$oci_user = 'YOUR_DB_USER';
$oci_pw = 'YOUR_DB_PASSWORD';
$oci_db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 000.000.000.000)(PORT = 1521)))(CONNECT_DATA=(SID=XXX)))"; 

// Get data from JSON imput
$data = json_decode(file_get_contents("php://input"),true);

// Now you can do some checks on date etc.
$img = $data['IMAGE'];
$jfid = $data['OBJECT_ID'];
$jfdate = $data['DATE'];

// Let's beginn with the blob upload
// We have 3 fiels in our table: OBJECT_ID,DATE,BLOBIMAGE
// First you fill your BLOB with an 'Empty' one and assign in PL/SQL style :img
$sql = "INSERT INTO ZAEHLER.METERAPP_BILD (OBJECT_ID,DATE,BLOBIMAGE)
VALUES (".$jfid.",to_date('".$jfdate."','DD/MM/YYYY'),empty_blob())
RETURNING BLOBIMAGE INTO :img";

$result = oci_parse($conn, $sql);
$blob = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($result, ":img", $blob, -1, OCI_B_BLOB);
oci_execute($result, OCI_NO_AUTO_COMMIT);

// Now let's check if we could connect to database or if we have to output something => 500
if(!$result){
    $err = oci_error();
    header('HTTP/1.0 500 Internal Server Error');
    header('Content-Type: application/json');
    $out = array('code' => '500', 'response' => '500 Internal Server Error / SQL connection problem', 'error sql' => $err[message]);
    echo json_encode($out);
}
// Can we same the image ($img) or not => 406
// This step saves the image to the db
if(!$blob->save($img)) {
    oci_rollback($conn);
    header('HTTP/1.0 406 Not Acceptable');
    header('Content-Type: application/json');
    $out = array('code' => '406', 'response' => '406 Not Acceptable / Wrong image type or JSON error');
    echo json_encode($out);
}
// If both was ok, we're going to commit and output an OK => 200
else {
    oci_commit($conn);
    header('HTTP/1.0 200 OK');
    header('Content-Type: application/json');
    $out = array('code' => '200', 'response' => '200 OK', 'response advanced' => 'Image saved', 'object_id' => $jfid, 'file date' => $jfdate);
    echo json_encode($out);
}

// Clean up
oci_free_statement($result);
$blob->free();
?>
于 2018-07-12T08:25:30.870 回答