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

yii2单个文件上传:

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

model层:

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

<?php

namespace app\models;
use Yii;
use yii\base\Model; 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 app\models; use Yii;
use yii\base\Model; 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 app\controllers; use Yii;
use yii\web\Controller;
use app\models\Upload;
use app\models\UploadForm;
use yii\web\UploadedFile; class TestController extends Controller{ public function actionIndex(){
return $this->renderPartial('index');
} /**
* @return string|\yii\web\Response
* 单文件上传
*/
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 yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<!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 yii\helpers\Html;  
use yii\widgets\ActiveForm; ?>
<!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>  

yii2.0单文件上传和多文件上传的更多相关文章

  1. yii2.0的gradview点击按钮通过get方式传参

    1.直接看views层里的代码就可以了 , <!--?= GridView::widget([ 'dataProvider' =--> $dataProvider, 'filterMode ...

  2. Yii2.0.7 限制user module登录遇到的问题

    在Yii2.0.6的时候我是在以下文件通过以下方法实现的. frontend/modules/user/Module.php namespace frontend\modules\user; clas ...

  3. 8.Yii2.0框架控制器接收get.post数据

    8.Yii2.0框架控制器接收get.post数据 一.get传参 <?php /** * Created by Haima. * Author:Haima * QQ:228654416 * D ...

  4. YII2.0上传文件

    针对于YII2.0官方手册来说,我稍微修改了一些内容具体的就是把model层里定义的uoload方法在controller方法里合并了 创建模型 namespace app\models; use y ...

  5. Yii2表单提交(带文件上传)

    今天写一个php的表单提交接口,除了基本的字符串数据,还带文件上传,不用说前端form标签内应该有这些属性 <form enctype="multipart/form-data&quo ...

  6. asp.net.mvc 的单文件上传和多文件上传的简单例子

    首先打开vs2012,创建空的mvc4项目,名称为MVCStudy,选择基本模板

  7. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  8. 文件的上传(表单上传和ajax文件异步上传)

    项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举的主要对于小文件上传的处理! 资源下载: 一. ...

  9. 普通文件的上传(表单上传和ajax文件异步上传)

    一.表单上传: html客户端部分: <form action="upload.ashx" method="post" enctype="mul ...

随机推荐

  1. 一步一步深入spring(5)--使用基于注解的spring实现 AOP

    1.要利用spring aop,至少需要添加以下jar包 使用spring需要的jarspring.jar .commons-logging.jar 使用切面编程(AOP)需要的jar aspectj ...

  2. Jwalk发布——一个比较小的Js动画库

    断断续续折腾了几个晚上终于于周日把Jwalk发布了,顺便用了下yahoo的前端框架-pure css ,很简洁,非常帅.推荐使用以下. 下面说下Jwalk是做什么的: 前端开发过程中经常会用到一些动画 ...

  3. WCF 学习笔记之双工实现

    WCF 学习笔记之双工实现 其中 Client 和Service为控制台程序 Service.Interface为类库 首先了解契约Interface两个接口 using System.Service ...

  4. 【Win32API】SendInput ERROR_BUSY 错误原因

    最近需要解决一个Windows上模拟键盘输入的问题, 使用SendInput这个API来实现的.当我从另外一台机器给当前机器发送一条键盘指令时,发现SendInput一直是成功的,但是没有看到任何输入 ...

  5. the selected server is enabled,but is not configured properly.Deployment to it will not be permitted

    用Tomcat添加部署项目的时候报错: the selected server is enabled,but is not configured properly.Deployment to it w ...

  6. 【C++自我精讲】基础系列一 指针与引用

    [C++自我精讲]基础系列一 指针与引用   一 前言   指针.引用.指针与引用区别. 二 指针   变量:代码中常常通过定义变量来申请并命名存储空间,并通过变量的名字来使用这段存储空间. //变量 ...

  7. 根据字节码探讨java自增运算符的原理

    public class Test { static int x, y; public static void main(String args[]) { x++; myMethod(); Syste ...

  8. WPF星空效果

    效果 前阵子看到ay的蜘蛛网效果和知乎的登录页背景,觉得效果很酷.自己也想写一个.于是写着写着就变成这样了.少女梦幻的赶脚有木有.我这有着一颗少女心的抠脚大汉 实现思路 分为两个部分: 1.星星无休止 ...

  9. floor()函数 向下取整 ceil()函数向上取整

    floor(x)  is the largest integer not greater than x , 也就是,floor(x) 返回的是小于等于x的所有整数中最大的整数,简单的说,就是去掉x的小 ...

  10. 十件你需要知道的事,关于openstack-trove(翻译)

    开源数据库即服务OpenStack Trove应该知道的10件事情 作者:Ken Rugg,Tesora首席执行官 Ken Rugg是Tesora的创始人,CEO和董事会成员. Ken的大部分职业都是 ...