Database 将图像上载到数据库
我是Yii的新手,我遇到了一些问题 我正在尝试按照以下步骤将图像上载到数据库。 然而,我似乎无法让它工作。它总是看到没有任何东西被上传 以下是我的模型代码:Database 将图像上载到数据库,database,image,yii,Database,Image,Yii,我是Yii的新手,我遇到了一些问题 我正在尝试按照以下步骤将图像上载到数据库。 然而,我似乎无法让它工作。它总是看到没有任何东西被上传 以下是我的模型代码: public $uploadedFile; /** * Saves the name, size, type and data of the uploaded file */ public function beforeSave() { if($file=CUploadedFile::getInstance($this,'uplo
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>
有人能帮忙吗?您的
操作创建中不需要下一行:
$model->image = CUploadedFile::getInstance($this,'image'); // remove it
您的后期处理应该如下所示
if (isset($_POST['Subdivision']))
{
$model->attributes = $_POST['Subdivision'];
if ($model->save())
$this->redirect(array('view','id'=>$model->subdivision));
}
还要确保数据库中的image
字段具有相应类型的BLOB来存储二进制数据
编辑,工作样本
型号(仅剩下重要零件)
控制器
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(); ?>
在数据库中保存图像的代码
型号
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>
请删除图像-规则中必需。是图像字段为BLOB类型,但问题仍然存在。保存时还是说“图像不能为空”。哦,我明白了。您是否在$form=$this->beginWidget('CActiveForm',数组(…)中定义了'htmlOptions'=>array('enctype'=>'multipart/form data')
?感谢您的回复,是的,我在表单中也定义了,但它仍然看不到回复感谢,是的,我在表单中也定义了HTMLOPTION,但它仍然无法上传任何内容。您如何在模型中定义规则?
<?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'));