我对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>
任何人都可以帮忙吗?
答案 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'));
请删除规则中要求的图片。