使用 PHP 在 Oracle 中将图像上传为 BLOB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11970258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload images as BLOBs in Oracle using PHP
提问by Shubham Saini
Can anyone please tell me how to store images in an oracle database as BLOBs using PHP?
谁能告诉我如何使用 PHP 将图像作为 BLOB 存储在 oracle 数据库中?
A working example would be nice. Thankyou.
一个工作示例会很好。谢谢你。
回答by DRiFTy
You first need to get the image that is being uploaded from the $_FILES#global array:
您首先需要从$_FILES#global 数组中获取正在上传的图像:
$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)) {
oci_rollback($connection);
}
else {
oci_commit($connection);
}
oci_free_statement($result);
$blob->free();
回答by FixFaier
Thank you DRiFTy, I build my small JSON query on your example. Let's clarrify that you can save large images with both examples.
谢谢 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();
?>
回答by celsowm
You can use PDO too:
您也可以使用 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();

