dphg63476 2015-03-11 16:55
浏览 53
已采纳

将文件存储到mySql BLOB Yii中

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

  • 写回答

1条回答 默认 最新

  • dou426098 2015-03-13 14:42
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探