簡體   English   中英

使用php從數據庫存儲/提取圖像

[英]Storing / Extracting images from database with php

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

為了存儲我正在這樣做:

 $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,但不起作用。

有人知道我如何使用php下載圖像?

不管怎么說,還是要謝謝你...

顯然,您的自定義getBinary_Data()返回$ file_info ['binary_data']作為資源,而不是字符串。.因此,請更改函數或使用base64_decode(file_get_contents($file_info['binary_data'])));

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

PS在postgre中沒有二進制數據的blob類型嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM