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(
    'id'=>'expenses-form',
    'enableAjaxValidation'=>false,
    'method'=>'post',
    'type'=>'horizontal',
    'htmlOptions'=>array(
        'enctype'=>'multipart/form-data'
    )
     ));
 echo $form->fileField($model,'binaryfile');

step7:write some code to in your controller action “Create” to store the uploaded image to your blob fie

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

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)))

Nintriva

Leave a Reply

Your email address will not be published. Required fields are marked *