You are viewing revision #3 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.
Save file using blob ¶
After this article that explains how to save a file in the filesystem, here an article for do the same using a blob field in 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 receivig the file from the form
* It should be different fron any other field in 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, because has to be safe. Is also a good practice to delete any rule refering the other 4 fields (file_name, file_type, file_size, file_content), because 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: ¶
For 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;
}
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
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?
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?
re
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
Displaying images inline
It should be noted that images can be displayed inline via the data URI scheme.
displaying in view
It doesn't work. I get the following hyperlink displayed in view:
/connectUm/index.php/candidate
which points to:
/connectUm/index.php/candidate/displaySavedImage/1
but no image is displayed.
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"
viewing the image
image is not displayed. some one help me to display image from database.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.