Hi,
I am trying to store images in table (field type - blob) & Display the same in a view when requested.
I went through following links in YII WIKI topics:
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
http://www.yiiframework.com/wiki/95/saving-files-to-a-blob-field-in-the-database/
I think I am able to store the image in database… (A record for which Blob field contains 197670763584419_100000244909867_697429_4864212_n.jpg)…
But i am not able to display the image. (Second wiki link talks about this.)
Please help me find a solution.
My controller:
public function actionCreate()
{
$model=new example;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['example']))
{
$model->attributes=$_POST['example'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
//$model->image->saveAs('path/to/localFile');
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
public function actionDisplaySavedImage()
{
$model=$this->loadModel($_GET['id']);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header('Content-length: '.$model->file_size);
header('Content-Type: '.$model->file_type);
header('Content-Disposition: attachment; filename='.$model->file_name);
echo $model->file_content;
}
My _form:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'example-form',
'enableAjaxValidation'=>true,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'profile'); ?>
<?php echo $form->textArea($model,'profile',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'profile'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'image'); ?>
<?php echo $form->fileField($model, 'image'); ?>
<?php echo $form->error($model, 'image'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My Model:
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'image'))
{
$this->file_name=$file->name;
$this->file_type=$file->type;
$this->file_size=$file->size;
$this->file_content=file_get_contents($file->tempName);
}
return parent::beforeSave();
}
public function rules()
{
return array(
....
array('image', 'file', 'allowEmpty' => false, 'types' => 'jpg, jpeg, gif, png'),
....
);
}
My view:
<?php echo CHtml::link(my_link_name,array('displaySavedImage','id'=>$model->primaryKey)); ?>
Thank you.
HBK