• 7

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Upload images as BLOBs in Oracle using PHP

Can anyone please tell me how to store images in an oracle database as BLOBs using PHP?

A working example would be nice. Thankyou.

You first need to get the image that is being uploaded from the $_FILES #global array:

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

Then to insert the image in the database try this:

$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)) {
else {

  • 16
Reply Report

Thank you DRiFTy, I build my small JSON query on your example. Let's clarrify that you can save large images with both examples.

// Database parameters
$oci_user = 'YOUR_DB_USER';
$oci_pw = 'YOUR_DB_PASSWORD';

// 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
VALUES (".$jfid.",to_date('".$jfdate."','DD/MM/YYYY'),empty_blob())

$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
    $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)) {
    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 {
    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
  • 0
Reply Report

You can use PDO too:


   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');
  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions