2011-02-27 246 views
0

我在数据库中的blob字段中有图像,我用blow colde来显示图像,但是 它没有显示任何图片,有人可以告诉我我的错在哪里?显示存储在MySQL数据库中的图像的问题?

这是我的模型:

public function getListUser() { 

     $select = $this->select() 
        ->order('lastname DESC'); 
     $adapter = new Zend_Paginator_Adapter_DbTableSelect ($select); 
     return $adapter; 
    } 

这是我的控制器:

$userModel = new Admin_Model_User(); 
$adapter = $userModel->getListUser(); 
$paginator = new Zend_Paginator ($adapter); 
$paginator->setItemCountPerPage(1); 
$page = $this->_request->getParam('page', 1); 
$paginator->setCurrentPageNumber($page); 
$this->view->paginator = $paginator; 

这是我的看法代码:

<td style="width: 20%;"> <?php 
    echo $this->lastname ?> </td> 

<td style="width: 20%;"><?php header("Content-type: image/gif"); print $this->image; ?></td> 

回答

4

这不是你如何显示你的图像。

你的代码应该看起来像这样。

<td style="width: 20%;"> <?php 
    echo $this->lastname ?> </td> 

<td style="width: 20%;"><img src="/link-to-php-that-shows-image.php?id=123" /></td> 

现在,以php文件为例(link-to-php-that-shows-image.php)。您从数据库中获取图像字段并显示它。例子..

<?php 
$image = //get image from database, based on id etc. 
header("Content-type: image/gif"); 
print $image; 
?> 
-2

对于初学者有一个非常方便的规则:

PHP无法在纯HTML中做任何事情。

现在回答自己一个问题:

  • ,你会看到什么,如果你直接粘贴图像文件的内容转换成HTML代码?

然后又是一个

  • 如何图像显示在HTML?

而且最后一个:

  • 你确定要存储在BLOB字段图像数据库?
+2

事实上,他可以。通过构造。但在这种情况下,它会相当尴尬。我认为。 – akond 2011-02-27 08:20:04

0

你不能做这行代码。

<td style="width: 20%;"> 
    <?php header("Content-type: image/gif"); print $this->image;?> 
</td> 

这是因为,<td style="width: 20%;">是它获取图像之前打印的outputed和headers被发送到输出图像的HTML元素,所以你得到错误

Error:: Cannot modify header information - headers already sent by .. 

但是你不要在Zend的这个错误框架,因为错误显示可能已关闭,因此请查看您的application.ini文件进行设置。

做到这将是存储在文件夹中的图像和存储filename或数据库filepath和回声出类似

<td style="width: 20%;"> 
    <img src="<?php echo $imagefullpath; ?>"/> 
</td> 

但最好的方法,如果你仍然要存储在数据库中的图像,然后,你可以使用ajax加载图像。如需更多帮助,请发表评论。

相关问题