【问题标题】:Unable to store Image in BLOB无法将图像存储在 BLOB 中
【发布时间】:2019-11-21 22:20:42
【问题描述】:

我正在尝试使用 php 和 oracle 12c 将我的图像存储到我的数据库中,但它不允许我插入而不给出任何错误

ORDER_ID                                  NOT NULL NUMBER(38)
RX_IMAGE1                                 NULL BLOB
RX_IMAGE3                                 NULL BLOB
RX_IMAGE4                                 NULL BLOB
CREATE_DATE_TIME                          NULL DATE
LAST_UPDATE_DATE_TIME                     NULL DATE
RX_IMAGE2                                 NULL BLOB
public function prc()
{
    if ($this->input->post('send_prescription') == 'send_prescription') {
        $prescriptionInfo['rx_image1'] = addslashes(file_get_contents($_FILES['pic']['tmp_name']));
        $result = $this->PatientModel->setPrescription($prescriptionInfo);
        if ($result == TRUE) {
            $this->session->set_flashdata('new_val', '1');
        } else {
            $this->session->set_flashdata('new_val', '0');
        }
    }
    $customer_id = $this->session->userdata('CUSTOMER_ID');
    $data['patient_data'] = $this->PatientModel->getPatientsByCustid($customer_id);
    $this->load->view('prescription/new_prescription', $data);
}

没有报错

【问题讨论】:

  • 最好不要将图像存储在数据库中,数据库应该存储图像的链接。
  • 你做得怎么样?发布您正在使用的代码。只有 Oracle SQL/PL/SQL 语句。
  • ``` 公共函数 prc(){ if($this->input->post('send_prescription')=='send_prescription'){ $prescriptionInfo['rx_image1'] = addlashes(file_get_contents ($_FILES['pic']['tmp_name'])); $result = $this->PatientModel->setPrescription($prescriptionInfo); if($result==TRUE){ $this->session->set_flashdata('new_val','1'); }else{ $this->session->set_flashdata('new_val','0'); } } $customer_id = $this->session->userdata('CUSTOMER_ID'); $data['patient_data'] = $this->PatientModel->getPatientsByCustid($customer_id); $this->load->view('prescription/new_prescription',$data); } ```
  • 请edit问题并在此处添加格式正确的代码。 cmets 中的代码不可读。此外,这似乎是 PHP 代码,如果是这样,addslashes() 是破坏二进制数据的好方法(并且问题被错误标记)。

标签: php oracle oracle12c


【解决方案1】:

普通的INSERT INTO 不适用于 BLOB 插入。您必须使用标准方法。请仔细观察下面的代码示例。

表格

CREATE TABLE t (id NUMBER, img BLOB);   
INSERT INTO t VALUES (1, EMPTY_BLOB()); --A simple blob to test

目录

CREATE OR REPLACE DIRECTORY MY_DIR AS '/tmp/images';
GRANT READ, WRITE ON DIRECTORY MY_DIR TO user_name;

要插入的代码

DECLARE
  src_lob  BFILE := BFILENAME('MY_DIR', 'keyboard.jpg'); 
      --MY_DIR refers to the directory in the server where your blob files are stored.
     --It should have been created and relevant permissions be given to your schema 
     --as shown above.
  dest_lob BLOB;
BEGIN
  INSERT INTO t VALUES(2, EMPTY_BLOB())
     RETURNING img INTO dest_lob;

  DBMS_LOB.OPEN(src_lob, DBMS_LOB.LOB_READONLY);
  DBMS_LOB.LoadFromFile( DEST_LOB => dest_lob,
                         SRC_LOB  => src_lob,
                         AMOUNT   => DBMS_LOB.GETLENGTH(src_lob) );
  DBMS_LOB.CLOSE(src_lob);

  COMMIT;
END;
/

对于 PHP,请参阅 PHP 开发人员指南下的 this documentation,它基本上解释了使用上述技术所需的代码/步骤。

【讨论】:

  • 我使用下面的代码中插入分贝插入文件到PRESCRIPTION_IMAGES(ORDER_ID,RX_IMAGE1,CREATE_DATE_TIME,LAST_UPDATE_DATE_TIME)值('26' , '89504e470d0a1a0a5c305c305c300d494844525c305c3001305c305c30011508065c305c305c30679ab9765c305c305c3001735247425c30aece1ce95c305c305c300467414d415c305c30b18f0bfc61055c305c305c3009704859735c305c300ec'); 跨度>
  • 我正在将图像转换为十六进制值
  • @Sushantkr.kunwar :我已经更新了链接,请检查。无需转换为十六进制值。使用建议的方法。
  • 如果您可以访问数据库文件系统,这个答案是可以的,但许多开发人员没有。它也很复杂。只需使用stackoverflow.com/questions/11970258/…中给出的PHP解决方案@
猜你喜欢
  • 2010-12-26
  • 1970-01-01
  • 2019-04-03
  • 2015-09-04
  • 2013-07-19
  • 2013-04-17
  • 2021-05-06
  • 2012-05-31
  • 1970-01-01
相关资源
最近更新 更多