我是yii的新手,所以我跟着这个tutorial。
当我尝试上传图片时,它会报告空字段,即使它不是。
我的_form视图:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'picture-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
...
<div class="row">
<?php echo $form->labelEx($model,'path_to'); ?>
<?php echo $form->fileField($model,'path_to'); ?>
<?php echo $form->error($model,'path_to'); ?>
</div>
...
<?php $this->endWidget(); ?>
</div><!-- form -->
这是我的行动方法:
public function actionCreate()
{
$model=new Picture;
if(isset($_POST['Picture']))
{
$model->attributes=$_POST['Picture'];
$model->picture=CUploadedFile::getInstance($model,'path_to');
if($model->save()){
$log->lwrite('in save'.$model->picture);
$model->picture->saveAs(Yii::app()->basePath.'/../images/'.$model->picture);
$this->redirect(array('view','id'=>$model->id));
$log->lclose();
}
}
$this->render('create',array('model'=>$model,));
}
当我print_r($ _ FILES)那里应该有的东西 当我print_r($ _ POST)时,字段'path_to'为空,验证器可能正在挑选那个。
我可能在这里遗漏了一些东西而且我一无所知。
UPDATE1: 我注意到yii正在使用与文件输入同名的隐藏字段,而不是从$ _POST读取属性,这导致引擎读取空隐藏字段。 据我所知,当用户没有输入新图片时,隐藏字段可供更新。 任何人都可以建议什么是上传图片的最佳方式?
UPDATE2: 型号代码: class Picture扩展了CActiveRecord {
public $picture;
...
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('path_to, page_id', 'required'),
array('page_id', 'numerical', 'integerOnly'=>true),
array('alt_text', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, alt_text, path_to, page_id', 'safe', 'on'=>'search'),
//array('path_to', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
//array('path_to', 'length', 'max'=>255, 'on'=>'insert,update'),
array('path_to', 'unsafe'),
);
}
...
最佳!
答案 0 :(得分:3)
我认为,因为你已经宣布'path_to'在你的规则中是不安全的,大量的分配线:
$model->attributes=$_POST['Picture'];
将失败,这将导致规则验证失败。更改规则以允许path_to安全,你应该好好去。 。 。
您似乎还需要一个page_id,我在您的表单中没有看到。
答案 1 :(得分:1)
_form file for file upload字段:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'post-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<div class="row">
<?php echo $form->labelEx($model, 'image');?>
<?php echo $form->fileField($model, 'image'); ?>
<?php echo $form->error($model, 'image'); ?>
</div>
<?php $this->endWidget(); ?>
控制器代码:
public function actionCreate()
{
$model=new Post;
if(isset($_POST['Post']))
{
$model->attributes=$_POST['Post'];
$uploadedFile=CUploadedFile::getInstance($model,'image');
$fileName = $uploadedFile;
$model->image = $fileName;
if($model->save()){
$uploadedFile->saveAs('C:/Program Files/Apache Group/Apache2/htdocs'.Yii::app()->baseUrl.'/images/thumbnails/'.$fileName);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
diplay视图代码:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'ajaxUpdate' => false,
'columns'=>array(
array(
'name'=>'Thumbnail',
'value'=>array($model,'showThumbnail'), // function in model
'type'=>'html',
'htmlOptions'=>array('class'=>'thumbnail'),
),
)
));
?>
型号代码:
place this function in ur model :
public function showThumbnail($data){
$path = $data->image;
return CHtml::image(Yii::app()->baseUrl.'/images/thumbnails/'.$path); // same path as ur uploaded file path
}