2011-10-23 52 views
0

嘗試將此Blob保存到文件並將其加載爲圖像時遇到很大麻煩。如何將BLOB保存爲可讀文件,如.txt或.jpg?

使用SQLite管理器(Firefox插件)我能夠「另存爲」一個文件與我的圖像BLOB的內容。結果是一個奇怪的(對我來說)代碼。

因爲我不能發佈「文件的來源」,所以我附上了一個png的例子。

example

在我的Mac,保存的文件沒有extention,但我可以看到它產生的縮略圖圖像。

所以我想達到同樣的結果保存一個文件,但我得到的是一個16個字節的文件,我不能讀...

$pic = fopen('pics/thumbnails/pic_'.$id.'', 'w'); 
fwrite($pic, base64_encode($theFile)); 
fclose($pic); 

*編輯*

$theFile = shell_exec("sqlite3 AddressBookImages.sqlitedb 'select data from ABThumbnailImage where record_id = ".$id."'"); 
if($theFile != '') { 
    file_put_contents('pics/thumbnails/pic_'.$id.'.jpg', $theFile); 
} 
+0

'base64_encode'在這裏是一個確定的錯誤。但是'$ theFile'是什麼? – Jon

+0

哦,$ theFile是sqlite查詢的結果$ theFile = $ row ['data']; – Pluda

+1

你應該試試'file_put_contents('pics/thumbnails/pic _'。$ id,$ theFile)'。如果這不起作用,那麼涉及數據庫的問題就會出錯。 – Jon

回答

0

嘗試使用(b代表二進制)

$pic = fopen('pics/thumbnails/pic_'.$id.'', 'wb'); 
+0

我的確用wb試過,也沒有運氣 – Pluda

0

我找到一個解決這一點,但它很慢,我發佈了一個新問題,要求幫助改善這一點。

$sql2 = shell_exec("sqlite3 ".$endereco_iphone."".$db_pics." 'select hex(data) from ABThumbnailImage where record_id = ".$id."'"); 
// 
if($sql2 !='' && $sql2 != 'NULL') { 
    $img = ''; 
    foreach(explode("\n",trim(chunk_split($sql2,2))) as $h) { 
     $img .= chr(hexdec($h)); 
    } 
    if(file_put_contents('pics/thumbnails/pic_'.$id.'.jpg', $img)) { 
     $cnt .= "<img class='pics' src='pics/thumbnails/pic_".$id.".jpg' width='30px' height='30px'></img>"; 
    } 
} else { 
    $cnt .= "<div class='pics'></div>"; 
} 
相關問題