2012-03-26 62 views
1

我使用PHP 5.3.5和PostgreSQL。我正在從數據庫中存儲和提取圖像。使用php存儲/從數據庫中提取圖像

爲了存儲我這樣做:

$escaped_data = base64_encode(file_get_contents($_FILES['fileUpload']['tmp_name'])); 
$fileModel->setBinary_Data($escaped_data); 

它的工作,我在我的數據庫(BYTEA場)接收到的圖像。

的問題是提取這一點,我試圖將此代碼提取我的圖片:

$file_info = $fileModel->getBinary_Data($id_file); // This function return the binary_data of the image 

header('Content-Type: image/jpeg;base64'); 
header('Content-Disposition: attachment; filename=' . $file_info['_name']); 
base64_decode($file_info['binary_data'])); 

當我下載的圖片,我無法看到圖像...

回聲在:

echo base64_decode($file_info['binary_data']); 

此發生:

http://imageshack.us/f/18/encodez.jpg/

之後,我嘗試使用base64_decode內的函數stream_get_contents,但doens't工作。

有人知道我可以用php下載我的圖片嗎?

感謝反正...

+0

有什麼理由不將文件存儲在文件系統中? – 2012-03-26 22:40:22

+0

是的,我需要在數據庫中存儲圖像,因爲我會實現一個OCR系統。而OCR系統需要讀取數據庫中的圖像。 – KillCloud 2012-03-27 00:28:10

+0

有人有另一種解決方案嗎? – KillCloud 2012-03-27 03:02:35

回答

0

很明顯嘛定製getBinary_Data()返回$ FILE_INFO [「binary_data」]作爲一種資源,而不是字符串..所以要麼改變功能或使用base64_decode(file_get_contents($file_info['binary_data'])));

並嘗試那麼也使用fclose($file_info['binary_data']);

P.S.在Postgre中沒有二進制數據的blob類型?

+0

我收到了與file_get_contents相同的錯誤消息:「file_get_contents()期望參數1是字符串,給出的資源是」 – KillCloud 2012-03-26 23:15:36

+0

'stream_get_contents($ file_info ['binary_data'])''? – 2012-03-27 06:37:08

+0

它的工作,但下載圖像被打破。在我的代碼下面:echo base64_decode(stream_get_contents($ file_info ['binary_data'])); – KillCloud 2012-03-27 12:18:41