2014-01-23 57 views
0

我有两个文件;检索要查看的BLOB .bin图像

image.php

<?php 
// include db connect class 
require_once __DIR__ . '/db_connect.php'; 

// connecting to db 
$db = new DB_CONNECT(); 


$result = mysql_query("SELECT *FROM products") or die(mysql_error()); 
//Your code to call database for image id (from _GET['id']), for instance: 
$image_id = $_GET['pid']; 
$data = mysql_query("Select img where pid = $image_id"); 

while($data = mysql_fetch_assoc($result)){ 
    $image=$data['img']; 
    header("content-type: image/png"); 
    echo $image; 
} 
?> 

和ay.html查看图像;

<img src="image.php?pid=IMAGE_ID" /> 

很多例子我试过后,在本网站和其他地方,我仍然得到这个错误每个线程;

The image"http....../image.php?pid=IMAGE_ID" cannot be displayed because it contains errors. 

任何帮助,将不胜感激。 顺便说一句,MySql变量是一个叫做img 的LONGBLOB我想知道为什么没有一个简单的IMG类型用于MySQL变量!

+0

将图像存储在数据库中通常是/大部分时间都是错误的。数据库应该保持对存储在文件系统中的映像的位置的引用。这就是为什么大多数RDBMS中没有IMG类型。 – dendini

+0

不幸的是我意识到,但我怎样才能看到一个HTML页面上的blob? – raklar

回答

0

你应该发布你如何存储图像,以确定你的代码有什么问题,但由于错误说图像有某种错误,因此它没有正确存储到数据库。

通常从图像到二进制数据去和扭转步骤如下:

图像到二进制的转换:

<? php 

$image_data=file_get_contents('test.jpg'); 

$encoded_image=base64_encode($image_data); 

echo $encoded_image; 

?> 

二进制到图像转换:

<?php 

$image_data = file_get_contents('test.jpg'); 

$encoded_image = base64_encode($image_data); 

$decoded_image = base64_decode($encoded_image); 

echo $decoded_image; 

?> 

什么你完全错过了base_64编码,以防止符号被错误地转换。