Yii 1.1: 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. 1)binaryfile(blob) 2)fileName(varchar(100)) 3)fileType(varchar(50)) 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 controller.ie

public function actionloadImage($id)
        $this->renderPartial('image', array(
    Note:make sure that this action is inside your controller acess rules.ie
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

Total 7 comments

#18743 report it
Patrice at 2014/12/23 05:05am
Thanks a lot


#14826 report it
Felipe_Moreira at 2013/09/12 03:43pm

Thank you very much dude!

Finally one tutorial that worked perfectly!

#9676 report it
MAHESH.E.U at 2012/09/03 04:12am
good one.

Thank you sirin for posting such useful tutorials

#8747 report it
Rahmat Awaludin at 2012/06/23 10:10am
well, nice articel

thank you, terima kasih, hatur nuhun.. :D

#8718 report it
sirin k at 2012/06/21 03:10am

you just need 3 fields for this image upload.ie 1)binaryfile(blob) 2)fileName(varchar(100)) 3)fileType(varchar(50))

#8715 report it
Dedy Isnandar at 2012/06/20 08:49pm
Databases ?

Can you show the field in Expenses Databases ?

Cause i get error Properti "Image.fileName" is not defined

#8224 report it
bit0123 at 2012/05/19 10:09am
Thanks a lot

Very useful tut.

Leave a comment

Please to leave your comment.

Write new article