2015-10-15 125 views
1

在我的MySQL数据库中,我有一行LongBlob类型,名为photoYii上传的blob图像不显示

以我形式I用于Yii提供的fileField所以它看起来像这样:

<div class="row"> 
    <?php echo $form->labelEx($model,'photo'); ?> 
    <?php echo $form->fileField($model,'photo'); ?> 
    <?php echo $form->error($model,'photo'); ?> 
</div> 

我有2个数据库出于安全原因(1后端,而另一个用于前),所以我使用以显示代码它看起来像这样:

db = mysqli_connect("localhost","root","","hygeia_master"); //keep your db name 
$sql = "SELECT * FROM about_photo order by datetime desc limit 1"; 
$sth = $db->query($sql); 
$result=mysqli_fetch_array($sth); 
echo '<img src="data:image/jpeg;base64,'.base64_encode($result['photo']).'"/>'; ?> 

然后,它只是显示这个:

enter image description here

但是,无论何时从phpMyAdmin本身上传,它都会显示正确的图像。

回答

1

您需要在显示图像之前设置header()

首先,你需要创建siteController

喜欢这个动作,

public function actionloadImage() 
{ 
     db = mysqli_connect("localhost","root","","hygeia_master"); //keep your db name 
     $sql = "SELECT * FROM about_photo order by datetime desc limit 1"; 
     $sth = $db->query($sql); 
     $result=mysqli_fetch_array($sth); 
     header('Content-Type: image/jpeg'); 
     print $result['photo']; 
     exit(); 
} 

,并鉴于文件中使用此

echo CHtml::image(Yii::app()->controller->createUrl('/site/loadImage'), 'No Image', array('width'=>80,'height'=>70)); 
+0

整个页面消失只有图标与破碎的形象保持... – anyabythestars

+0

发生任何错误? – GAMITG

+0

不只是那一个图标......:/ – anyabythestars