Working with BLOB field in ORACLE using Codeigniter [closed]

0

I have a system where I will store certain files in a BLOB-type column in my ORACLE database, but I did not find anything about it in Codeigniter's own documentation on how to do this, how can I manipulate these files, where I need to feed this table and also view the file that is stored in that table?

    
asked by anonymous 24.10.2018 / 15:26

1 answer

1

Here is an example of how to update a BLOB column with a file received via upload. The secret is the concatenation of 0x with the string transformation of the binary content to hexadecimal.

public function updateColunaBlob($id_cliente, $file_name, $file_data) {
    $this->db->set('FILE_NAME', $file_name, false);
    $this->db->set('FILE_DATA','0x'.bin2hex($file_data), false);
    $this->db->where('ID_CLIENTE', $id_cliente, false);
    $this->db->update('CLIENTES');
}

But if the file is too large and there is no possibility to configure this limit, you can upload the file in small pieces without loss of content. Example for MySQL, which instead of 0x uses only X in front of the hexadecimal string. Here in a very explicit way; query builder is not being used and the column is updated from 512 Kb to 512 KB:

public function updateColunaBlob($id_cliente, $file_name, $file_data) {
    $handle = fopen($_FILES['nome_arquivo_upload']['tmp_name'], "rb");
    $contents = '';
    $i = 0;
    while (!feof($handle)) {
        $contents = fread($handle, 512000);

        if(!$i)
            $this->db->query("
                UPDATE
                    CLIENTES
                SET
                    FILE_NAME = ?,
                    FILE_DATA = X?
                WHERE
                    ID_CLIENTE = ?
            ", array($file_name, bin2hex($contents), $id_cliente));     
        else
            $this->db->query("
                UPDATE
                    CLIENTES
                SET
                    FILE_DATA = CONCAT(FILE_DATA, X?)
                WHERE
                    ID_CLIENTE = ?
            ", array(bin2hex($contents), $id_cliente));     
        $i++;
    }
    fclose($handle);
}
    
25.10.2018 / 19:49