mysql_real_escape_string is making error here . These two link will help you to handle this link1 and link2
so please use $this->db->escape($val) instead of mysql_real_escape_string
 <?php
    // <= PHP 5
    $data = file_get_contents($_FILES["userfile"]["tmp_name"], true);
    // > PHP 5
    $data = file_get_contents($_FILES["userfile"]["tmp_name"], FILE_USE_INCLUDE_PATH);
    ?>
$_FILES["userfile"]["tmp_name"] produce file name like 
<?php
// <= PHP 5
 $data = $this->db->escape(base64_encode(file_get_contents("test/faruk.txt", true)));
// > PHP 5
 $data = $this->db->escape(base64_encode(file_get_contents("test/faruk.txt", FILE_USE_INCLUDE_PATH)));
echo $data; 
?>
and my output is SGVsbG8gd29ybGQxNDEyMTYyMTYx for content "Hello world1412162161"