Yii上传图片 [英] Yii uploading pictures
问题描述
我是 yii 的新手,所以我关注了这个 教程.
当我尝试上传图片时,它会报告空字段,即使它不是.
我的 _form 视图:
<?php $form=$this->beginWidget('CActiveForm', array('id'='图片格式','enableAjaxValidation'=>false,'htmlOptions' =>数组('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');?>...<?php $this->endWidget();?></div><!-- 形式-->
这是我的操作方法:
公共函数 actionCreate(){$model=新图片;if(isset($_POST['图片'])){$model->attributes=$_POST['图片'];$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' 为空时,验证器可能正在选择那个.
我可能在这里遗漏了一些东西,而且我一无所知.
更新1:我注意到 yii 使用与文件输入同名的隐藏字段,而不是从 $_POST 读取属性,这导致引擎读取空的隐藏字段.我知道当用户没有输入新图片时,隐藏字段会更新.任何人都可以建议上传图片的最佳方式是什么?
更新2:型号代码:类图片扩展了 CActiveRecord{
public $picture;...公共函数规则(){//注意:你应该只为那些属性定义规则//将接收用户输入.返回数组(数组('path_to, page_id', 'required'),数组('page_id', 'numerical', 'integerOnly'=>true),数组('alt_text','安全'),//以下规则由 search() 使用.//请删除那些不应搜索的属性.数组('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'),数组('path_to','不安全'),);}...
最好!
我想是因为你已经在你的规则中声明了path_to"是不安全的,大量的赋值行:
$model->attributes=$_POST['图片'];
会失败,这将导致规则验证失败.更改您的规则以允许 path_to 是安全的,您应该很高兴...
看来您还需要一个 page_id,我在您的表单中没有看到.
I'm a new to yii, so I was following this tutorial.
when I try to upload picture it reports empty field even if it is not.
my _form view:
<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 -->
and here is my action method:
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,));
}
when I print_r($_FILES) everything what should be there is there when I print_r($_POST) the field 'path_to' is empty and the validator probably is picking that one.
I'm probably missing something here and I'm clueless.
update1: I've noticed yii is using hidden field with the same name as file input, than attributes are read from $_POST, which is causing the engine reading empty hidden field. I understand that the hidden field is there for update when user is not entering new picture. Can anyone advice what is the best way of doing picture upload?
update2: model code: class Picture extends 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'),
);
}
...
Best!
I think because you've declared 'path_to' to be unsafe in your rules, the massive assignment line:
$model->attributes=$_POST['Picture'];
Will fail, which will then cause the rule validation to fail. Change your rules to allow for path_to to be safe, and you should be good to go . . .
It also appears you're requiring a page_id, which I'm not seeing in your form.
这篇关于Yii上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!