【问题标题】:Error Inserting Image in Oracle BLOB with Codeigniter使用 Codeigniter 在 Oracle BLOB 中插入图像时出错
【发布时间】:2013-11-29 18:47:43
【问题描述】:

我正在尝试使用 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);
}

【问题讨论】:

    标签: image oracle codeigniter blob


    【解决方案1】:

    在 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);
      }
    ?>
    

    【讨论】:

      【解决方案2】:

      datatype 使用 LONGBLOB 中的问题 BLOB

      CREATE TABLE photos (
         id    int,
         photo longblob
      );
      

      【讨论】:

        猜你喜欢
        • 2015-09-12
        • 1970-01-01
        • 2021-06-01
        • 2020-12-26
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2019-06-09
        • 2019-11-19
        相关资源
        最近更新 更多