Storing your images in your table's blob field and displaying that stored images.

He im giving you a very simple example ie how you can store an image to your blob field in a table.Hope you already familiar with the normal image upload.

First im going to tell you the necessary steps you need to carry out to store an image into your blob field.

Let me explain these 7 steps ,

step1:define 3 fields in your table you want to store the image.


step2:generate model for this table using your gii code generator.

step3:Define some rules for the filed "binaryfile" inside your model

array('binaryfile', 'file', 
		'types'=>'jpg, gif, png, bmp, jpeg',
	        'maxSize'=>1024 * 1024 * 10, // 10MB
                'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',
	        'allowEmpty' => true

step4:generate CRUD for your model using your gii tool

step6:Update your _form.php inside views like below

     $form = $this->beginWidget('bootstrap.widgets.BootActiveForm', array(
     )); ?>  

     <?php echo $form->fileField($model,'binaryfile'); ?>

step7:write some code to in your controller action "Create" to store the uploaded image to your blob field.

public function actionCreate()
		$model=new Expenses;
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

				$file = CUploadedFile::getInstance($model,'binaryfile');
				$model->fileName = $file->name;
				$model->fileType = $file->type;
				$fp = fopen($file->tempName, 'r');
				$content = fread($fp, filesize($file->tempName));
				$model->binaryfile = $content;
			$model->user = Yii::app()->user->id;


Now what you need to know is that how you can display an image stored inside your blob field.

step1:Write a new action inside your

public function actionloadImage($id)
		$this->renderPartial('image', array(
    Note:make sure that this action is inside your controller acess
array('allow', // allow authenticated user to perform 'create' and 'update' actions

step2: write a view file for the action "loadimage" you wrote above. ie image.php

header('Content-Type: ' . $model->fileType);
print $model->binaryfile; 

step3:now you can easly display this image from your blob field in any of your rendered views like below.

echo CHtml::image(Yii::app()->controller->createUrl('expense/loadImage', array('id'=>$this->model->id))); 

-Sirin k