如何使用Yii框架2.0上传多个文件 [英] How to upload multiple files using Yii framework 2.0

查看:150
本文介绍了如何使用Yii框架2.0上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Yii框架2.0我想能够上传多个文件。遵循Yii 2 文档,在上传多个文件



  class Newsletter扩展\yii\db\ActiveRecord { 
public $ attachment_file;

public function rules()
{
return [
[['attachment_file'],'file','maxFiles'=> 5],
]; ($ this-> validate()){
foreach($ this-> attachment_file as $ file){
echo'< pre>;的print_r($文件);回声'< / pre>';
}
返回true;
} else {
return false;





$ b

以下是我的观点。 / p>

 <?php使用yii\widgets\ActiveForm; ?> 
$ b<?php $ form = ActiveForm :: begin(['options'=> ['enctype'=>'multipart / form-data']])?>>

<?= $ form-> field($ model,'attachment_file []') - > fileInput(['multiple'=> true,])?>

<按钮>提交< / button>

<?php ActiveForm :: end()?>

在我的控制器中,我有下面的代码片段:
$ b $如果(Yii :: $ app-> request-> isPost){
$ model-> attachment_file = UploadedFile :: getInstances($ model,'

  attachment_file'); 
if($ model-> upload()){
die();
//文件上传成功
return;






$ b

以上所有的代码我希望我可以选择多个具有一个输入文件元素的文件。但它不像我所期望的那样。当我选择多个文件与一个相同的输入文件元素,并点击提交我只看到了最后选定的文件。所以我开始怀疑我在做什么。我做错了什么吗?或者我需要多次添加输入文件元素,一个输入文件元素用于一个上传文件? 解决方案

看看我试过:
view code


 <?php $ form = ActiveForm :: begin(['options'=> ['enctype'=>'multipart / form-data']])?>> 

<?= $ form-> field($ uploadForm,'files []') - > fileInput(['multiple'=> true])?>

< button class =btn btn-primary>上传< /按钮>
<?php ActiveForm :: end()?>

在控制器中

 使用yii\web\UploadedFile; 
使用app \models\MultipleUploadForm;
使用app \models\ProductImage;
.......
函数actionUploadImage(){
$ form = new MultipleUploadForm();
$ b $ if(Yii :: $ app-> request-> isPost){
$ form-> files = UploadedFile :: getInstances($ form,'files'); ($ form-> files&& $ form-> validate()){
foreach($ form->文件为$ file){$ b $

b $ image = new ProductImage();
if($ image-> save()){
$ file-> saveAs($ image-> getPath());





return $ this-> render('uploadImage',[
' uploadForm'=> $ form,
]);

$ / code> $ / pre>

MultipleUploadForm模型

 使用yii\base\Model; 
使用yii\web\UploadedFile;

class MultipleUploadForm extends Model
{
/ **
* @var UploadedFile []文件上传
* /
public $ files;

/ **
* @return数组验证规则。
* /
public function rules()
{
return [
[['files'],'file','extensions'=> 'jpg','mimeTypes'=> 'image / jpeg','maxFiles'=> 10,'skipOnEmpty'=>假],
];




$ b

这段代码正在为我工​​作。希望这也适用于你。


Working with Yii framework 2.0 I want to be able to upload multiple files. Following Yii 2 documentation, under subsection Upload Multiple Files I have the following model.

class Newsletter extends \yii\db\ActiveRecord {
    public $attachment_file;

    public function rules()
    {
         return [
              [['attachment_file'], 'file', 'maxFiles' => 5],
         ];
    }

    public function upload() {

        if ($this->validate()) { 
            foreach ($this->attachment_file as $file) {
                echo '<pre>'; print_r($file); echo '</pre>';
            }
            return true;
        } else {
            return false;
        }
    }
}

Below is my view.

<?php use yii\widgets\ActiveForm; ?>

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

<?= $form->field($model, 'attachment_file[]')->fileInput(['multiple' => true,]) ?>

<button>Submit</button>

<?php ActiveForm::end() ?>

In my controller I have the following code snippet.

if (Yii::$app->request->isPost) {
        $model->attachment_file = UploadedFile::getInstances($model, 'attachment_file');
        if ($model->upload()) {
            die();
            // file is uploaded successfully
            return;
        }
}

With all the code above I expect I can select multiple files with one input file element. But it is not like what I expect. When I select multiple files with one same input file element and hit Submit I saw only the last selected file. So I start to have doubt about what I am doing. Did I do anything wrong? Or do I need to add input file element several times, one input file element for one uploading file?

解决方案

See what I tried: view code

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

    <?= $form->field($uploadForm, 'files[]')->fileInput(['multiple' => true]) ?>

    <button class="btn btn-primary">Upload</button>
    <?php ActiveForm::end() ?>

in controller

    use yii\web\UploadedFile;
    use app\models\MultipleUploadForm;
    use app\models\ProductImage;
    .......
    function actionUploadImage() {
        $form = new MultipleUploadForm();

        if (Yii::$app->request->isPost) {
            $form->files = UploadedFile::getInstances($form, 'files');

            if ($form->files && $form->validate()) {
                foreach ($form->files as $file) {
                    $image = new ProductImage();
                    if ($image->save()) {
                        $file->saveAs($image->getPath());
                    }
                }

            }
        }

        return $this->render('uploadImage', [
            'uploadForm' => $form,
        ]);
    }

MultipleUploadForm model

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

class MultipleUploadForm extends Model
{
    /**
     * @var UploadedFile[] files uploaded
     */
    public $files;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
        [['files'], 'file', 'extensions' => 'jpg', 'mimeTypes' => 'image/jpeg', 'maxFiles' => 10, 'skipOnEmpty' => false],
        ];
    }
}

This code is working for me. Hope this works for you too.

这篇关于如何使用Yii框架2.0上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆