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

Changes

Title changed

Saving files into a blob field in the database

Category unchanged

Tips

Yii version unchanged

Tags changed

fFile upload

Content changed

Save file using blob
 
------------------
 
 
After [this article
As a follow-up from the [How to upload a file using a model](http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/ "How to upload a file using a model") wiki entry that explains how to save a file into the filesystem, here anthis article forwill 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 fronm any other field in the database */ public $uploadedFile;          public function rules()         {
 
            return array(
 
                
{
 
return array(
 
array('uploadedFile', 'file', 'types'=>'jpg, gif, png'),             );
 
        
);
 
} /** *s 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, because 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), becauseas they are not supposed to be modified by massively assigned inputs. #### Edit the view: In the view we can put:
 
 

```php
<div class="row">
[...]
### Displaying images:

For show theIn the view we put the code:
 
 
 
```php 
<? 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: ```php /** * Displays the preview of the photoOpens the dialog in browser to open/save the image. */ public function actionDisplaySavedImage() { $model=$this->loadModel();
 
header('Content-Type: '.$model->file_type);
 
$_GET['id']);
 
 
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.
6 2
17 followers
Viewed: 79 750 times
Version: 1.1
Category: Tips
Written by: zaccaria
Last updated by: zaccaria
Created on: Nov 3, 2010
Last updated: 13 years ago
Update Article

Revisions

View all history