Yii2.0 多文件上传
---------------------------------------------------------------------------------------------------
首先你得调整模型类,在 file
验证规则里增加一个 maxFiles
选项,用以限制一次上传文件的最大数量。
upload()
方法也得修改, 以便一个一个地保存上传的文件。
---------------------------------------------------------------------------------------------------
<?php
namespace app\models;
use yii\base\Model;
use yii\web\UploadedFile;
class UploadForm1 extends Model{
/**
* @var UploadedFile[]
*/
public $imageFiles;
public function rules()
{
return [
[['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
];
}
public function upload()
{
foreach ($this->imageFiles as $file) {
$file->saveAs('./upload/' . $file->baseName . '.' . $file->extension);
}
return true;
}
}
--------------------------------------------------------------------------------------------------------------
在视图文件里,你需要把 multiple
选项添加到fileInput()
函数调用里, 这样文件输入控件就可以接收多个文件。
---------------------------------------------------------------------------------------------------------------
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<table class="table table-bordered table-hover definewidth m10">
<tr>
<td class="tableleft">上传多个图片</td>
<td><?= $form->field($model, 'imageFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
</td>
</tr>
<tr>
<td class="tableleft"></td>
<td>
<button type="submit" class="btn btn-primary" type="button">保存</button>
</td>
</tr>
<?php ActiveForm::end() ?>
-------------------------------------------------------------------------------------------------------------
最后,在控制器的 action 方法中,你应该调用 UploadedFile::getInstances()
而不是 UploadedFile::getInstance()
来把UploadedFile
实例数组赋值给 UploadForm::imageFiles
。
------------------------------------------------------------------------------------------------------------
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use app\models\UploadForm1;
use yii\web\UploadedFile;
class PhotoController extends Controller
{
public $enableCsrfValidation = false;
public function actionIndex()
{
$request = Yii::$app->request;
$model = new UploadForm1();
if($request->isPost)
{
$file=$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
//print_r($file);die;
foreach ($file as $key => $v) {
$image[]=$v->name;
}
//print_r($image);die;
$images=implode(',',$image);
if($model->upload())
{
$connection = \Yii::$app->db;
$connection->createCommand()->insert('upload', [
'image' => $images,
])->execute();
}
}
else
{
return $this->render('upload1',['model' => $model]);
}
}
}
Yii2.0 多文件上传的更多相关文章
- yii2.0单文件上传和多文件上传
yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传: yii2单个文件上传: 上传步奏,先创建上传表单模型model(包含验证规 ...
- Servlet3.0学习总结——基于Servlet3.0的文件上传
Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileu ...
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ...
- java-基于Servlet3.0的文件上传
Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet3.0中使用request.getParts()获取上传文件
- Servlet3.0之八:基于Servlet3.0的文件上传@MultipartConfig
在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ...
- phpcms v9.6.0任意文件上传漏洞(CVE-2018-14399)
phpcms v9.6.0任意文件上传漏洞(CVE-2018-14399) 一.漏洞描述 PHPCMS 9.6.0版本中的libs/classes/attachment.class.php文件存在漏洞 ...
- servlet3.0的文件上传代码配置怎么写
之前学习过xml配置servlet3.0的文件上传,但是变成code方式一直不知道怎么弄,相比较起来apache的文件上传配置和xml倒是没什么太大区别. 直接上代码:无需依赖,只要一个方法就好了cu ...
- Servlet3.0 multipart 文件上传技术
Servlet3.0 javaConfig配置 传统的servlet都是在web.xml中配置,从Servlet 3.0开始提供了ServletContainerInitializer接口,允许使用代 ...
- Spring boot2.0 设置文件上传大小限制
今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...
随机推荐
- (转) Written Memories: Understanding, Deriving and Extending the LSTM
R2RT Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...
- A Flock Of Tasty Sources On How To Start Learning High Scalability
This is a guest repost by Leandro Moreira. When we usually are interested about scalability we look ...
- Oracle数据库——半期测验
一.使用system用户登录SQL*PLUS,使用命令将scott用户解锁,并将scott用户的密码修改为: t_你的学号后三位(例如:t_165).然后,以scott用户连接数据库. 1. 使用sy ...
- linux概念之IPC
一切皆文件,文件类型为普通,目录,管道,socket,链接,块设备,字符设备cd /proc/2305/fd/ 该进程打开了6个文件描述符,三个为字符设备即012,三个为socket即345,sock ...
- unity, GL.TexCoord or GL.Color must put before GL.Vertex!!!
GL.Begin(GL.QUADS); //in unity, should use left hand rule //RU GL.TexCo ...
- html之内联标签img
img创建的是被链接图像的占位空间,它不会真正地在网页插入图像. 两个必须属性: src:图像的超链接 alt:图像的替代文本 可选属性: height:高度 width:宽度 ismap:将图像定义 ...
- 每天一个linux命令-转载
每天一个linux命令目录 转载自: http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令 ...
- 阿里云Mysql重置密码
1.关闭mysql服务 # service mysql stop 如果提示mysql: unrecognized service这样的错误提示. 先查看查找mysql.server,使用:find / ...
- 真实赛车3,SPEEDRUSH TV 第3季,第3阶段(第3天),直线加速赛
与其跳过,不如金币升级引擎和车身.因为后边紧跟一场计时赛.
- 剑指offer系列38----滑动窗口的最大值(不懂????????????????????????????????????????????????)
[题目] 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6 ...