Difference between #11 and #10 of Saving files to a blob field in the database

Saving files to a blob field in the database
File upload
As a follow-up from the [How to upload a file using a
"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:


So it generates link like

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.
Write new article
  • Written by: zaccaria
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +9 / -3
  • Viewed: 58,230 times
  • Created on: Nov 3, 2010
  • Last updated: Nov 4, 2010
  • Tags: File upload