Yii文件上传在控制器中给出空数组

时间:2014-05-27 11:54:54

标签: php file-upload yii

您好,请检查我的以下代码:

型号:

 public $image;
/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'file';
}

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('image_url', 'required'),
        array('image_url', 'length', 'max'=>256),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('image_url', 'safe', 'on'=>'search'),
         array('image', 'file', 'types'=>'jpg, gif, png'),
    );
}

的Controler:

public function actionCreate()
{
    $model=new File;
            print_r($_POST['File']);
             $this->render('create',array(
        'model'=>$model,
    ));
}

查看:

<?php
 $form = $this->beginWidget(
'CActiveForm',
array(
    'id' => 'upload-form',
    'enableAjaxValidation' => false,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);

   echo $form->labelEx($model, 'image');
   echo $form->fileField($model, 'image');
   echo $form->error($model, 'image');
   echo CHtml::submitButton('Submit'); 
   $this->endWidget();?>

您可以在我的控制器中看到 print_r($ _ POST [&#39;文件&#39;]); 。它给了我空数组,没有关于上传图像的网址或任何内容。请帮忙

由于

解决方案

对控制器功能进行了以下更改,这对我有用。

public function actionCreate()
{
    $model=new File;
            $model->image_url = CUploadedFile::getInstance($model,'image');


            if($model->save())
            $model->image_url->saveAs('D:/xampp/htdocs/project/images/upload/logo.jpg');


    $this->render('create',array(
        'model'=>$model,
    ));
}

1 个答案:

答案 0 :(得分:1)

$model = new Photo;
$model->attributes = $_POST['Photo'];
$model->image = CUploadedFile::getInstance($model,'image');