Этот пример используется в интерфейсе REST 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();
?>