0

can't add a photo to FIrebird. Writing such a code

        $imgSrc='Desert.jpg';
    $img_src = $imgSrc;
    $imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
    $img_str = base64_encode($imgbinary); 

    $blh = ibase_blob_create($this->db);   
    ibase_blob_add($blh, $img_str);
    $blobid = ibase_blob_close($blh);

    $row = false;
    /*$fd = fopen('Desert.jpg', 'r');
    $blob = ibase_blob_import($fd);
    fclose($fd); */
    $query = ibase_query($this->db, "INSERT INTO \"ud_ab\" (FILES) VALUES (?)", $img_str ) or die(ibase_errmsg());
    if($query) $row = true; 
    return $row;

Tried to translate the picture in base64 format, wrote ibase_blob_add.Nothing helps

1
  • 1
    Instead of $img_str, I think you should pass $blobid (or maybe $blh) to ibase_query. Note that it is not necessary to base64 encode this, as a blob is - by default - binary data. Note that I don't program in PHP, I'm just guessing based on this question: stackoverflow.com/questions/28801781/… Commented Aug 16, 2017 at 13:18

1 Answer 1

1

This is sample code for saving images, but in blob field

define('MAX_SEGMENT_SIZE', 65535);

function blob_create($data) {
    if (strlen($data) == 0)
        return false;
    $handle = ibase_blob_create();
    $len = strlen($data);
    for ($pos = 0; $pos < $len; $pos += MAX_SEGMENT_SIZE) {
        $buflen = ($pos + MAX_SEGMENT_SIZE > $len) ? ($len - $pos) : MAX_SEGMENT_SIZE;
        $buf = substr($data, $pos, $buflen);
        ibase_blob_add($handle, $buf);
    }
    return ibase_blob_close($handle);
}
$blob = blob_create(file_get_contents('Desert.jpg'));
$query = ibase_query($this->db, "INSERT INTO \"ud_ab\" (FILES) VALUES (?)", $blob) or die(ibase_errmsg());
Sign up to request clarification or add additional context in comments.

1 Comment

I believe that in some older Firebird versions, the max segment size might be limited to 32K instead of 64K.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.