使用Codeigniter在Oracle BLOB中插入映像时出错

时间:2013-11-16 09:42:14

标签: image oracle codeigniter blob

我正在尝试使用Codeigniter框架和BLOB数据类型将图像插入到oracle数据库中。但是,我尝试的一切都给了我以下两个错误:

  

遇到PHP错误   严重性:警告   消息:oci_parse():ORA-00972:标识符太长   文件名:oci8 / oci8_driver.php

     

遇到PHP错误   严重性:警告   消息:oci_set_prefetch()期望参数1是资源,给定布尔值   文件名:oci8 / oci8_driver.php

这是我的数据库表:

CREATE TABLE photos (
   id    int,
   photo blob
);

我的控制器功能:

function do_upload() {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';

    $this->load->library('upload', $config);

     if ( $this->upload->do_upload() ){
         $upload_data = $this->upload->data();
         $this->upload_model->upload_photo($upload_data['full_path']);
     }
     else {
         echo $this->upload->display_errors();
     }
}

我的模特功能:

public function upload_photo($full_path) {   
    $fp = fopen($full_path, 'r'); 
    $image  = fread($fp, filesize($full_path)); 
    fclose($fp); 

    $sql = "INSERT INTO images ('id', 'photo')
             VALUES ('45', '" . $image . "')";
    $this->db->query($sql);
}

2 个答案:

答案 0 :(得分:0)

在PHP中处理Oracle LOB比处理简单数据类型要复杂一些。首先,您需要创建某种类型的新OCI描述符,然后在SQL语句中使用它。有关详细信息,请参阅此链接:http://php.net/manual/en/function.oci-new-descriptor.php

这是来自php.net的简单代码示例:

<?php
/* This script demonstrates file upload to LOB columns
 * The formfield used for this example looks like this
 * <form action="upload.php" method="post" enctype="multipart/form-data">
 * <input type="file" name="lob_upload" />
 * ...
 */
  if (!isset($lob_upload) || $lob_upload == 'none'){
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Upload file: <input type="file" name="lob_upload" /><br />
<input type="submit" value="Upload" /> - <input type="reset" value="Reset" />
</form>
<?php
  } else {

     // $lob_upload contains the temporary filename of the uploaded file

     // see also the features section on file upload,
     // if you would like to use secure uploads

     $conn = oci_connect($user, $password);
     $lob = oci_new_descriptor($conn, OCI_D_LOB);
     $stmt = oci_parse($conn, "insert into $table (id, the_blob)
               values(my_seq.NEXTVAL, EMPTY_BLOB()) returning the_blob into :the_blob");
     oci_bind_by_name($stmt, ':the_blob', $lob, -1, OCI_B_BLOB);
     oci_execute($stmt, OCI_DEFAULT);
     if ($lob->savefile($lob_upload)){
        oci_commit($conn);
        echo "Blob successfully uploaded\n";
     }else{
        echo "Couldn't upload Blob\n";
     }
     $lob->free();
     oci_free_statement($stmt);
     oci_close($conn);
  }
?>

答案 1 :(得分:-1)

数据类型中的

问题使用 LONGBLOB 而不是 BLOB

CREATE TABLE photos (
   id    int,
   photo longblob
);