Yii 上传的 blob 图片不显示
Yii uploaded blob image is not displaying
在我的 MySQL 数据库中,我有一行名为 photo
的 LongBlob 类型。
在我的表单中,我使用了 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'] ).'"/>'; ?>
然后它只显示这个:
但是每当我从 phpMyAdmin 本身上传时,它都会显示正确的图像。
显示图片前需要设置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));
在我的 MySQL 数据库中,我有一行名为 photo
的 LongBlob 类型。
在我的表单中,我使用了 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'] ).'"/>'; ?>
然后它只显示这个:
但是每当我从 phpMyAdmin 本身上传时,它都会显示正确的图像。
显示图片前需要设置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));