将图像上传到数据库

时间:2012-01-16 06:31:14

标签: database image yii

我对Yii很新,我遇到了一些问题。

我试图在wiki之后将图片上传到数据库。 但是,我似乎无法使其发挥作用。它总是看到没有上传任何内容。

以下是我的模型代码:

public $uploadedFile;

/**
* Saves the name, size, type and data of the uploaded file
*/
public function beforeSave()
{
    if($file=CUploadedFile::getInstance($this,'uploadedFile'))
    {
        $this->image_name=$file->name;
        $this->image_type=$file->type;
        $this->image_size=$file->size;
        $this->image=file_get_contents($file->tempName);
    }
    return parent::beforeSave();
}

创建控制器:

public function actionCreate()
{
    $model=new Subdivision;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Subdivision']))
    {
        $model->attributes=$_POST['Subdivision'];
        $model->image = CUploadedFile::getInstance($this,'image');

        if($model->save())
            $this->redirect(array('view','id'=>$model->subdivision));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

和我的表单代码:

    <div class="row">
    <?php echo $form->labelEx($model,'uploadedFile'); ?>
    <?php echo $form->fileField($model,'uploadedFile'); ?>
    <?php echo $form->error($model,'uploadedFile'); ?>
    </div>

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

actionCreate

中不需要下一行
$model->image = CUploadedFile::getInstance($this,'image'); // remove it

您的POST处理应该是

if (isset($_POST['Subdivision']))
{
    $model->attributes = $_POST['Subdivision'];
    if ($model->save())
        $this->redirect(array('view','id'=>$model->subdivision));
}

还要确保数据库中的image字段具有相应的BLOB类型以存储二进制数据。


编辑,工作样本

模型(仅剩下重要部分)

public $uploadedFile;

public function rules()
{
    return array(
        array('uploadedFile', 'file', 'types'=>'jpg, gif, png'),
        array('image_name,image_type,image_size,image', 'safe')
    );
}

public function beforeSave()
{
    if ($file = CUploadedFile::getInstance($this, 'uploadedFile'))
    {
        $this->image_name = $file->name;
        $this->image_type = $file->type;
        $this->image_size = $file->size;
        $this->image = file_get_contents($file->tempName);
    }

    return parent::beforeSave();
}

<强>控制器

public function actionIndex()
{
    $model = new User(); /* I called my model User in your case it's Subdivision */

    if (isset($_POST['User']))
    {
        $model->attributes = $_POST['User'];
        $model->image = CUploadedFile::getInstance($this, 'image');

        if ($model->save())
            $this->redirect(array('index'));
    }

    $this->render('index', array(
        'model' => $model
    ));
}

查看

<?php $form = $this->beginWidget('CActiveForm', array(
    'htmlOptions' => array('enctype' => 'multipart/form-data')
)); ?>
    <div class="row">
        <?php echo $form->labelEx($model, 'uploadedFile'); ?>
        <?php echo $form->fileField($model, 'uploadedFile'); ?>
        <?php echo $form->error($model, 'uploadedFile'); ?>
    </div>
    <input type="submit" value="Save">
<?php $this->endWidget(); ?>

答案 1 :(得分:0)

在dataBase中保存图像的代码

<强>模型

public function beforeSave()
        {
            if($file=CUploadedFile::getInstance($this,'uploadedFile'))
            {
                $this->image=$file->name;
                $this->image=$file->type;
                $this->image=$file->size;
                $this->image=file_get_contents($file->tempName);
            }

        return parent::beforeSave();
        }

<强>表格

<?php $form = $this->beginWidget('CActiveForm', array(
    'htmlOptions' => array('enctype' => 'multipart/form-data')
)); ?>
    <div class="row">
    <?php echo $form->labelEx($model,'uploadedFile'); ?>
    <?php echo $form->fileField($model,'uploadedFile'); ?>
    <?php echo $form->error($model,'uploadedFile'); ?>
    </div>

将这些添加到模型

中的规则中
return array( array('uploadedFile', 'file', 'types'=>'jpg, gif, png'),
                    array('image_name,image_type,image_size,image', 'safe'));

请删除规则中要求的图片。