zoukankan      html  css  js  c++  java
  • yii2.0单文件上传和多文件上传

    yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传:

     

    yii2单个文件上传:

    上传步奏,先创建上传表单模型model(包含验证规则),其次控制器操作action,以及相对应的view:

    model层:

    Upload.php  [单文件上传模型]

    <?php

    namespace appmodels;
    use Yii;
    use yiiaseModel;

    class Upload extends Model{
    public $file;


    public function rules(){
    return [

    [['file'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',],
    ];
    }


    public function attributeLabels(){
    return [
    'file'=>'文件上传'
    ];
    }
    }

    UploadForm.php  [多文件上传模型]

    <?php
    namespace appmodels;

    use Yii;
    use yiiaseModel;

    class UploadForm extends Model
    {
    /**
    * @var UploadedFile|Null file attribute
    */
    public $file;

    /**
    * @return array the validation rules.
    */
    public function rules()
    {
    return [
    [['file'], 'file', 'maxFiles' => 10,'extensions'=>'jpg,png,gif'],
    ];
    }

    public function attributeLabels(){
    return [
    'file'=>'多文件上传'
    ];
    }

    Controller层,以TestController中的upload操作和upmore操作

    <?php  
    namespace appcontrollers;

    use Yii;
    use yiiwebController;
    use appmodelsUpload;
    use appmodelsUploadForm;
    use yiiwebUploadedFile;

    class TestController extends Controller{

    public function actionIndex(){
    return $this->renderPartial('index');
    }

    /**
    * @return string|yiiwebResponse
    * 单文件上传
    */
    public function actionUpload(){
    $model= new Upload();

    if (Yii::$app->request->isPost) {
    $file = UploadedFile::getInstance($model, 'file');
    $path='uploads/'.date("YmdH",time()).'/';
    if ($file && $model->validate()) {
    if(!file_exists($path)){
    mkdir($path,0775,true);
    }
    $file->saveAs($path . time() . '.' . $file->getExtension());
    Yii::$app->session->setFlash('success','上传成功!');
    return $this->redirect('upload');
    }
    }

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


    public function actionUpmore(){
    $model = new UploadForm();
    if (Yii::$app->request->isPost) {
    $file = UploadedFile::getInstances($model, 'file');

    if ($file && $model->validate()) {
    echo "<pre/>";

    foreach ($file as $fl) {
    $fl->saveAs('uploads/' .mt_rand(1100,9900) .time() .$fl->baseName. '.' . $fl->extension);
    }
    Yii::$app->session->setFlash('success','上传成功!');
    return $this->redirect('upmore');
    }
    }

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

    view:层

    [单文件view  uplod.php]

    <?php  
    use yiihelpersHtml;
    use yiiwidgetsActiveForm;
    ?>
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <h4>文件上传</h4>
    <?php if(Yii::$app->session->hasFlash('success')):?>
    <div class="alert alert-danger">
    <?=Yii::$app->session->getFlash('success')?>
    </div>
    <?php endif ?>
    <?php $form=ActiveForm::begin([
    'id'=>'upload',
    'enableAjaxValidation' => false,
    'options'=>['enctype'=>'multipart/form-data']
    ]);
    ?>
    <?= $form->field($model, 'file')->fileInput();?>
    <?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
    <?php ActiveForm::end(); ?>

    </body>
    </html>
    [多文件view upmore.php]
    <?php  

    use yiihelpersHtml;
    use yiiwidgetsActiveForm;

    ?>
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <h4>多文件上传</h4>
    <?php if(Yii::$app->session->hasFlash('success')):?>
    <div class="alert alert-danger">
    <?=Yii::$app->session->getFlash('success')?>
    </div>
    <?php endif ?>
    <?php $form=ActiveForm::begin([
    'id'=>'upload',
    'enableAjaxValidation' => false,
    'options'=>['enctype'=>'multipart/form-data']
    ]);
    ?>
    <?= $form->field($model, 'file[]')->fileInput(['multiple' => true]);?>
    <?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
    <?php ActiveForm::end(); ?>

    </body>
    </html>  
  • 相关阅读:
    用“Keras”11行代码构建CNN
    技术 | 使用深度学习检测DGA(域名生成算法)
    未来的超级智能网络攻击需要AI竞技俱乐部来拯救
    开源中国的代码托管
    Hello Java !
    15-include的使用
    14-递归函数
    13-函数的调用
    12-函数的返回值
    11-函数的参数
  • 原文地址:https://www.cnblogs.com/Pxhphp/p/6237025.html
Copyright © 2011-2022 走看看