【问题标题】:Yii File Upload giving empty array in controllerYii 文件上传在控制器中给出空数组
【发布时间】:2014-07-16 07:52:25
【问题描述】:

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

型号:

 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'),
    );
}

控制者:

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['File']);。它给了我没有 url 或任何关于上传图像的空数组。请帮忙

谢谢

解决方案

对适用于我的控制器功能进行了以下更改。

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,
    ));
}

【问题讨论】:

    标签: php file-upload yii


    【解决方案1】:
    $model = new Photo;
    $model->attributes = $_POST['Photo'];
    $model->image = CUploadedFile::getInstance($model,'image');
    

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多