如何在yii2

时间:2018-06-28 04:22:52

标签: yii2 yii2-basic-app

使用yii2时我遇到一个问题

$model = Client::find()->where('id=1')->one();
UploadedFile::getInstance($model, 'logo');

然后使用此gitme NULL值。如果我打印NULL并得到数组值,为什么会得到$_FILES。 我在yii2中使用了简单的上传文件

我的观点:

$form = ActiveForm::begin(['action' => ['center/updatelogo'], 'layout' => 'horizontal', 'options' => ['enctype' => 'multipart/form-data']]);
echo $form->field($model, 'logo')->widget(FileInput::classname(), [
    'options' => ['accept' => $model->logo, 'class' => 'form-control'],
    'pluginOptions' => [
        'allowedFileExtensions' => ['png', 'jpg'],
        'maxFileSize' => 25000,
    ],
]);

我的控制器:

$model = Client::find()->where('id=1')->one();
if (Yii::$app->request->isPost) {
    $model->file = UploadedFile::getInstance($model, 'logo');
    if ($model->validate()) {
        $model->file->saveAs('logos/' . $model->file->baseName . '.' . $model->file->extension);
    }
}
var_dump($file);die();

我需要将一个文件上传到mi服务器并贴上徽标,发生了什么事?

我的$ _POST和我的$ _FILES:

array(2) {
  ["_csrf"]=>
  string(88) "WF67odQ_DUylcUprRN9FssjEdq5nBn2ARrjjql_D8af6Y1am1ID7AEG5KV2iWNL-7QeEoeGF_UJQYxnH6JgS1A=="
  ["Center"]=>
  array(1) {
    ["logo"]=>
    string(0) ""
  }
}

array(1) {
  ["Center"]=>
  array(5) {
    ["name"]=>
    array(1) {
      ["logo"]=>
      string(19) "Libre Albedrío.jpg"
    }
    ["type"]=>
    array(1) {
      ["logo"]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(1) {
      ["logo"]=>
      string(14) "/tmp/phpYijyXv"
    }
    ["error"]=>
    array(1) {
      ["logo"]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      ["logo"]=>
      int(19900)
    }
  }
}

0 个答案:

没有答案