Saving files to a blob field in the database

You are viewing revision #4 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#3)next (#9) »

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()
	{
		if($file=CUploadedFile::getInstance($this,'uploadedFile'))
		{
			$this->file_name=$file->name;
			$this->file_type=$file->type;
			$this->file_size=$file->size;
			$this->file_content=file_get_contents($file->tempName);
		}

	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'); ?>
</div> 
Displaying images:

To show the image we can write an action:

/**
 * Displays the preview of the photo.
 */
public function actionDisplay()
{
	$model=$this->loadModel();
	header('Content-Type: '.$model->file_type);
	echo $model->file_content;
}
6 2
17 followers
Viewed: 80 817 times
Version: Unknown (update)
Category: Tips
Written by: zaccaria
Last updated by: zaccaria
Created on: Nov 3, 2010
Last updated: 14 years ago
Update Article

Revisions

View all history

Related Articles