Yii 1.1: Saving files to a blob field in the database


As a follow-up from the How to upload a file using a model wiki entry that explains how to save a file to the filesystem, this article will do the same using a blob field in the database.

Edit the model:

The first step is to change the model in order to self manage the file:

class Candidate extends CActiveRecord
     * Property for receiving the file from the form
     * It should be different from any other field in the database
    public $uploadedFile;
    public function rules()
        return array(
            array('uploadedFile', 'file', 'types'=>'jpg, gif, png'),
    * Saves the name, size, type and data of the uploaded file
    public function beforeSave()
    return parent::beforeSave();

Do not forget to give a rule for this attribute as it has to be safe. Is also a good practice to delete any rule referring to the other 4 fields (file_name, file_type, file_size, file_content), as they are not supposed to be modified by massively assigned inputs.

Edit the view:

In the view we can put:

<div class="row">
<?php echo $form->labelEx($model,'uploadedFile'); ?>
<?php echo $form->fileField($model,'uploadedFile'); ?>
<?php echo $form->error($model,'uploadedFile'); ?>

Displaying images:

In the view we put the code:

<? echo CHtml::link(my_link_name,array('displaySavedImage','id'=>$model->primaryKey)); ?>

So it generates link like http://myserver/yii/myapp/index.php?r=candidate/displaySavedImage&id=1

Where Candidate is current model.

To show saved image we can write an action in Controller:

 * Opens the dialog in browser to open/save the image.
public function actionDisplaySavedImage()
    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;

Note $_GET['id'] as passed argument of loadModel function.

Total 8 comments

#17427 report it
vijay p s at 2014/06/11 12:59am
viewing the image

image is not displayed. some one help me to display image from database.

#12595 report it
amerrnath at 2013/03/31 04:03am
displaying in view

Error 403

You are not authorized to perform this action.

im getting this error when i click the link to display in from link " http://localhost/yii/test3/index.php?r=patient/displaySavedImage&id=4"

#7298 report it
thammu at 2012/03/12 10:43am
displaying in view

It doesn't work. I get the following hyperlink displayed in view:


which points to:


but no image is displayed.

#5181 report it
Da:Sourcerer at 2011/09/21 11:04am
Displaying images inline

It should be noted that images can be displayed inline via the data URI scheme.

#5171 report it
zaccaria at 2011/09/21 02:07am

How do we display an image retrieved from a blob? See the last part of the tutorial, you have to create an action this porpouse.

Or save it to disk on the server? You have not to copy, you can create an action like actionDisplaySavedImage.

If you want to save directly in the disk, there is another tutorial about it, see here

#5169 report it
Alex Adams at 2011/09/20 04:27pm
Or save it to disk on the server?

If the answer to how to display the image from a blob is that we must temporarily copy it to a location on the server and reference it there, then how do we retrieve the blob and write to an image file on the server?

#5168 report it
Alex Adams at 2011/09/20 04:24pm
How do we display an image retrieved from a blob?

How do we take the image in the blob and get it to show up in an image tab or other container?

#4246 report it
smug at 2011/06/20 01:37am
displaying in view

when i run it i get error undefined index:id

and if i do print_r($_GET); to see what values are set i get an empty array

Leave a comment

Please to leave your comment.

Write new article
  • Written by: zaccaria
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +9 / -3
  • Viewed: 59,742 times
  • Created on: Nov 3, 2010
  • Last updated: Nov 4, 2010
  • Tags: File upload