简体   繁体   English

使用php从数据库存储/提取图像

[英]Storing / Extracting images from database with php

I am using PHP 5.3.5 and postgreSQL. 我正在使用PHP 5.3.5和postgreSQL。 I am storing and extracting images from database. 我正在从数据库中存储和提取图像。

For storing i am doing this: 为了存储我正在这样做:

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

It's working, i received the image in my database (Bytea field). 它正在工作,我在数据库(Bytea字段)中收到了图像。

The problem is to extract this, i am trying this code to extract my images: 问题是要提取这个,我正在尝试下面的代码提取我的图像:

$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']));

When i download the image, i can't see the image... 下载图片时,看不到图片...

With echo in: 在回显中:

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

This happen: 发生这种情况:

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

After, i am trying using the function stream_get_contents inside base64_decode, but doens't work. 之后,我尝试使用base64_decode内的函数stream_get_contents,但不起作用。

Someone know how i can download my images with php? 有人知道我如何使用php下载图像?

Thanks anyway... 不管怎么说,还是要谢谢你...

Well obviously your custom getBinary_Data() returns $file_info['binary_data'] as a resource, not a string.. so either change the function or use base64_decode(file_get_contents($file_info['binary_data']))); 显然,您的自定义getBinary_Data()返回$ file_info ['binary_data']作为资源,而不是字符串。.因此,请更改函数或使用base64_decode(file_get_contents($file_info['binary_data'])));

And try to use fclose($file_info['binary_data']); 并尝试使用fclose($file_info['binary_data']); too then. 那也是。

PS Wasn't there a blob type for binary data in postgre? PS在postgre中没有二进制数据的blob类型吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM