欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

yii2利用自帶UploadedFile實(shí)現(xiàn)上傳圖片的示例

 更新時(shí)間:2017年02月16日 16:04:59   作者:ony  
本篇文章主要介紹了yii2利用自帶UploadedFile實(shí)現(xiàn)上傳圖片的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本人搜索了很多關(guān)于yii2利用自帶UploadedFile實(shí)現(xiàn)上傳圖片介紹,下面我來記錄一下。

創(chuàng)建一個(gè) models/UploadForm.php:

namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

/**
* UploadForm is the model behind the upload form.
*/
class UploadForm extends Model
{
 /**
 * @var UploadedFile file attribute
 */
 public $file;

 /**
 * @return array the validation rules.
 */
 public function rules()
 {
  return [
   [['file'], ‘file'],
  ];
 }
}

視圖文件

<?php
use yii\widgets\ActiveForm;
?>

<?php $form = ActiveForm::begin([‘options' => ['enctype' => 'multipart/form-data']]) ?>

<?= $form->field($model, ‘file')->fileInput() ?>

<button>Submit</button>

<?php ActiveForm::end() ?>

控制器

use app\models\UploadForm;
use yii\web\UploadedFile;



public function actionUpload()
{
 $model = new UploadForm();

 if (Yii::$app->request->isPost) {
  $model->file = UploadedFile::getInstance($model, ‘file');

  if ($model->file && $model->validate()) {
   $model->file->saveAs(‘uploads/' . $model->file->baseName . ‘.' . $model->file->extension);
  }
 }

 return $this->render(‘upload', ['model' => $model]);
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論