I whant to know if i and how i can store the whole file in my DB wich looks like this
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| file | blob | NO | | NULL | |
| USER_id | int(11) | NO | MUL | NULL | |
| zname | varchar(40) | NO | | NULL | |
| date | date | NO | | NULL | |
| filetyp | varchar(10) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
I managed to make a file upload Widget without any funktion behind it. It is possible to select a file and to press the Submit button. But thats it.
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
echo $form->labelEx($model,'');
echo $form->fileField($model,'image');
echo $form->error($model,'image');
echo CHtml::submitButton('Submit');
$this->endWidget();
and now i dont know how to make a Controller Funktion that saves my file into my database. Where the "File"(DB filed) is the aktual kontent of the file the zname = the File name , date the aktual date of upload and filetype = the filetype.
For more information i had a question about downloading Files like that: Download link from Database BLOB with yii Framework
Ok what i did was add this to the view:
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
The Controller has $model devined as the USERZ module (DB) the form that is been called looks like this
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'userz-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<div class="row">
<?php echo $form->fileField($model,'file'); ?>
<?php echo $form->error($model,'file'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
Also i added a if clause to the Contoller:
if(isset($_POST['USERZ']))
{
$model->attributes=$_POST['USERZ'];
$model->file=CUploadedFile::getInstance($model,'file');
$model->beforeSave();
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
The bevore Save funktion is a funktion in the USERZ model wich looks like this and devines everything conzerning the file:
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'file'))
{
$this->zname=$file->name;
$this->filetype=$file->type;
$this->date=date('Y-m-d');
$this->USER_id=Yii::app()->user->getId();
$this->file=file_get_contents($file->tempName);
}
return parent::beforeSave();
}