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 ...
随机推荐
- 【转】NSString属性什么时候用copy,什么时候用strong?
原文网址:http://www.cocoachina.com/ios/20150512/11805.html 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境) ...
- unity, monoDevelop ide 代码提示不起作用的解决方法
monoDevelop ide 代码提示不起作用,可能是因为ide里索引了一些不存在的文件,检查一下solution窗口里是否有文件变红,如下图中springControlEx.cs.将变红的文件re ...
- Google Proposes to Enhance JSON with Jsonnet
Google has open sourced Jsonnet, a configuration language that supersedes JSON and adds new features ...
- 【转】用 PHP 内置函数 file_put_contents 写入文件
PHP 内置函数 file_put_contents 用于写入文件. file_put_contents 函数最简单的写法,可以只用两个参数,一个是文件路径,一个是要写入的内容,语法如下: file_ ...
- 一个项目中说系统分为表现层、控制层、逻辑层、DAO层和最终数据库五层架构-转
表现层就是看到的东西,比如你现在看到的当前页面控制层就将你的请求从页面传到后台代码逻辑层就是处理你的请求的代码DAO层就是将数据存到数据库中的代码数据库就是数据库了,存东西用的 ,DAO层就是将访问数 ...
- HBase(六): HBase体系结构剖析(上)
HBase隶属于hadoop生态系统,它参考了谷歌的BigTable建模,实现的编程语言为 Java, 建立在hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它仅能通过主键( ...
- Installing IPython using pip on CentOS
1. First to install pip # wget https://bootstrap.pypa.io/get-pip.py # python get-pip.py 2. Install i ...
- autotool相关:AC_ARG_ENABLE的用法
你可以使用AC_ARG_ENABLE来定义一个命令行选项.这个宏接受三个参数1.flag_base2.该选项的帮助说明3.当configure带该选项运行时所执行的代码,代码中的命令行变量enable ...
- DISPOSE_ON_CLOSE 和 EXIT_ON_CLOSE 的区别
If you have several JFrames open and you close one that has EXIT_ON_CLOSE it will close all the JFra ...
- iphone dev 入门实例5:Get the User Location & Address in iPhone App
Create the Project and Design the Interface First, create a new Xcode project using the Single View ...