gpt4 book ai didi

Yii上传图片

转载 作者:行者123 更新时间:2023-12-04 03:10:49 26 4
gpt4 key购买 nike

我是 yii 的新手,所以我关注了这个 tutorial .

当我尝试上传图片时,它报告为空字段,即使它不是。

我的_表单 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 -->

这是我的操作方法:

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”为空时,验证器可能正在选择那个。

我可能在这里遗漏了一些东西而且我一无所知。

更新一:我注意到 yii 使用与文件输入同名的隐藏字段,而不是从 $_POST 读取属性,这导致引擎读取空的隐藏字段。我知道当用户没有输入新图片时,隐藏字段会被更新。谁能建议上传图片的最佳方式是什么?

更新2:型号代码: 类图片扩展 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'),
);
}
...

最好!

最佳答案

我认为因为你已经在你的规则中声明'path_to'是不安全的,大量的分配行:

$model->attributes=$_POST['Picture'];

会失败,然后会导致规则验证失败。更改您的规则以允许 path_to 是安全的,您应该可以开始了。 . .

您似乎还需要一个 page_id,但我在您的表单中没有看到。

关于Yii上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11671626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com