2017-08-16 13 views
0

無法將照片添加到FIrebird。寫這樣的代碼如何在php中添加blob圖像FIrebird?

 $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; 

嘗試翻譯的Base64格式的圖片,寫道ibase_blob_add.Nothing幫助

+1

而不是' $ img_str',我想你應該把'$ blobid'(或者''blh'')傳遞給'ibase_query'。請注意,base64不需要對其進行編碼,因爲blob是默認情況下的二進制數據。請注意,我不用PHP編程,我只是猜測這個問題:https://stackoverflow.com/questions/28801781/php-firebird-insert-blob-file-into-the-database-apache-突然停止工作 –

回答

1

這是保存圖像的示例代碼,但在BLOB字段

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()); 
+0

我相信在一些較老的Firebird版本中,最大段大小可能限制爲32K而不是64K。 –