zoukankan      html  css  js  c++  java
  • [YII2] 文件上传类

     1 //测试文件上传类
     2 
     3    public function actionCreate()
     4     {
     5         $model = new Lvyou();
     6 
     7         $upload_model = new appmodelsUploadForm();  
     8 
     9         $post = Yii::$app->request->post();
    10 
    11         if (Yii::$app->request->isPost) {
    12             $upload_model->file = yiiwebUploadedFile::getInstance($upload_model, 'file');
    13             if ($upload_model->file && $upload_model->validate()) {
    14                 $filename ='./../../uploads/' . $upload_model->file->baseName . '.' . $upload_model->file->extension;
    15                 $upload_model->file->saveAs($filename);
    16                 $post['Lvyou']['file'] = $filename;
    17                 // $ext=$model->file->extension;//上传文件后缀名
    18                 // $name=$model->file->baseName;//上传文件名        
    19             }
    20         }
    21        // if (isset($filename)) {
    22        //      $post['Lvyou']['file'] = $filename;
    23        //  }else{
    24        //      $post['Lvyou']['file']='他是不是没上传图片呀';
    25        //  }
    26 
    27         if ($model->load($post) && $model->save()) {
    28             return $this->redirect(['view', 'id' => $model->id]);
    29         } else {
    30             return $this->render('create', [
    31                 'model' => $model,
    32                 'upload_model'=> $upload_model,
    33             ]);
    34         }
    35         
    36     }
     1 //视图层
     2 
     3 一键生成的在index
     4 
     5     <?= GridView::widget([
     6         'dataProvider' => $dataProvider,
     7         'filterModel' => $searchModel,
     8         'columns' => [
     9             ['class' => 'yiigridSerialColumn'],
    10 
    11             'id',
    12             'file'=>[
    13                 'attribute'=>'file',      
    14                 'format' => ['image',['width'=>'100','height'=>'80',]],
    15             ],
    16 
    17             ['class' => 'yiigridActionColumn'],
    18         ],
    19     ]); ?>
    20 
    21   在_form
    22 
    23  <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
    24 
    25 <?= $form->field($upload_model, 'file')->fileInput() ?>
    26 
    27 <?php ActiveForm::end(); ?>

    model层 UploadForm文件

     1 <?php
     2 namespace appmodels;
     3 
     4 
     5 use yiiaseModel;
     6 use yiiwebUploadedFile;
     7 
     8 /**
     9 * UploadForm is the model behind the upload form.
    10 */
    11 class UploadForm extends Model
    12 {
    13     /**
    14      * @var UploadedFile file attribute
    15      */
    16     public $file;
    17 
    18     /**
    19      * @return array the validation rules.
    20      */
    21     public function rules()
    22     {
    23         return [
    24             [['file'], 'file'],
    25         ];
    26     }
    27 }
  • 相关阅读:
    HDNOIP普及+提高整合
    [BZOJ4016][FJOI2014]最短路径树问题
    [BZOJ3697]采药人的路径
    [COJ0985]WZJ的数据结构(负十五)
    [KOJ6024]合并果子·改(强化版)
    [KOJ6023]合并果子·改
    [KOJ0574NOIP200406合并果子]
    Atomic operations on the x86 processors
    Javascript 严格模式详解
    const C语言(转)
  • 原文地址:https://www.cnblogs.com/lipcblog/p/6591094.html
Copyright © 2011-2022 走看看