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