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

10 followers

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

<?php
     $form = $this->beginWidget('bootstrap.widgets.BootActiveForm', array(
    'id'=>'expenses-form',
    'enableAjaxValidation'=>false,
    'method'=>'post',
    'type'=>'horizontal',
    'htmlOptions'=>array(
        'enctype'=>'multipart/form-data'
    )
     )); ?>  
 
     <?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);
 
        if(isset($_POST['Expenses']))
        {
            $model->attributes=$_POST['Expenses'];
 
            if(!empty($_FILES['Expenses']['tmp_name']['binaryfile']))
            {
                $file = CUploadedFile::getInstance($model,'binaryfile');
                $model->fileName = $file->name;
                $model->fileType = $file->type;
                $fp = fopen($file->tempName, 'r');
                $content = fread($fp, filesize($file->tempName));
                fclose($fp);
                $model->binaryfile = $content;
            }
 
            $model->user = Yii::app()->user->id;
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }
 
        $this->render('create',array(
            'model'=>$model,
            'types'=>Type::model()->findAll()
        ));
    }

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)
    {
        $model=$this->loadModel($id);
        $this->renderPartial('image', array(
            'model'=>$model
        ));
    }
    Note:make sure that this action is inside your controller acess rules.ie
array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update','loadImage','delete'),
                'users'=>array('@'),
),

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

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

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 6 comments

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

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
database

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
  • Written by: sirin k
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +8 / -1
  • Viewed: 22,354 times
  • Created on: May 15, 2012
  • Last updated: May 15, 2012
  • Tags: blob, image