2012-03-26 59 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