Reputation: 157
In my MySQL database I have a row with a LongBlob type called photo
.
In my form I used the fileField
provided by Yii so it looks like this:
<div class="row">
<?php echo $form->labelEx($model,'photo'); ?>
<?php echo $form->fileField($model,'photo'); ?>
<?php echo $form->error($model,'photo'); ?>
</div>
I have 2 databases for security reasons (1 for backend and another for front) so the code I use to display it looks like this:
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'] ).'"/>'; ?>
Then it only displays this:
But whenever I upload from phpMyAdmin itself it displays the correct image.
Upvotes: 1
Views: 290
Reputation: 3818
You need to set header()
before display image.
First you need to create action in siteController
Like this,
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();
}
And in view file use this
echo CHtml::image(Yii::app()->controller->createUrl('/site/loadImage'), 'No Image', array('width'=>80,'height'=>70));
Upvotes: 1