yii 多文件上传

跟手册相比 这里多了对文件大小的限制,希望对大家有帮助

因为跟单文件上传基本一样,我在原来的基础上做了修改  这里就直接上代码了

 

首先是model层

<?php
/**
 * Created by PhpStorm.
 * User: administor
 * Date: 2017/5/11
 * Time: 14:04
 */

namespace frontend\models;

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

class Upload extends Model
{
    public $file;
    public $maxsize = 1024*1024*10;
    public $minsize = 1024*20;
    public function rules(){
        return[
            ['file','file','maxSize'=>$this->maxsize,'minSize'=>$this->minsize,'maxFiles'=>3]
        ];
    }
   /* public function upload()
    {
        if ($this->validate()) {
            $this->file->saveAs('../../common/upload/' . $this->file->baseName . '.' . $this->file->extension);
            return true;
        } else {
            return false;
        }
    }*/
    public function checksize($size){
        if($size>$this->minsize && $size<$this->maxsize){
            return true;
        }else{
            return false;
        }
    } public function upload()
{
    if ($this->validate()) {
        foreach ($this->file as $file) {
            $filesize = $file->size;
            if($this->checksize($filesize)){
                $file->saveAs('../../common/upload/' . $file->baseName . '.' . $file->extension);
            }else{
                echo "filesize is wrong";
            }
            }
        return true;
    } else {
        return false;
    }
}
}










接下来是controller 控制器层

<?php
/**
 * Created by PhpStorm.
 * User: administor
 * Date: 2017/5/10
 * Time: 9:39
 */

namespace frontend\controllers;
use yii\web\Controller;
use yii;
use db;
use frontend\models\Form;
use frontend\models\Upload;
use yii\web\UploadedFile;
    class LoginController extends Controller
    {
            public function actionIndex(){
                $sql = 'select kid,kname from exam_tiku';
                $data = yii::$app->db->createCommand($sql)->queryAll();
                $arr = Form::dataarr($data);
                //var_dump($arr);die;
                $model = new Form();
                $models = new Upload();

                return $this->render('index',['model'=>$model,'data'=>$arr,'models'=>$models]);
            }
           /* public function actionAdd(){
                $data = Yii::$app->request->post();
                $model = new Upload();

                if (Yii::$app->request->isPost) {
                    $model->file = UploadedFile::getInstance($model, 'file');
                    $filesize =  $model->file->size;
                    if($model->checksize($filesize)){
                        if ($model->upload()) {
                            // 文件上传成功
                            return;
                        }
                    }else{
                        echo "filesize is not right";
                    }

              }
            }*/
        public function actionAdd()
        {
            $model = new Upload();

            if (Yii::$app->request->isPost) {
                $model->file = UploadedFile::getInstances($model, 'file');
                if ($model->upload()) {
                    // 文件上传成功
                    return;
                }
            }

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







然后是view层

<?php
/**
 * Created by PhpStorm.
 * User: administor
 * Date: 2017/5/10
 * Time: 9:41
 */
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal','enctype'=>'multipart/form-data'],
    'action'=>'?r=login/add',
    'method'=>'post'
]) ?>
    <?= $form->field($model, 'name') ?>
    <?= $form->field($model, 'pwd')->passwordInput() ?>
    <?= $form->field($model, 'sex')->radioList(['0'=>'男','1'=>'女']) ?>
    <?= $form->field($model, 'hobby')->checkboxList(['basketball'=>'篮球','baseball'=>'棒球','swim'=>'游泳']) ?>
    <?= $form->field($models, 'file[]')->fileInput(['multiple' => true, 'accept' => '']) ?>

<?= $form->field($model, 'age')->dropDownList($data,['prompt' => $data[3]]) ?>



    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
posted @ 2018-08-09 14:32  木楊  阅读(139)  评论(0编辑  收藏  举报