1. <small id='5C8kD'></small><noframes id='5C8kD'>

    <i id='5C8kD'><tr id='5C8kD'><dt id='5C8kD'><q id='5C8kD'><span id='5C8kD'><b id='5C8kD'><form id='5C8kD'><ins id='5C8kD'></ins><ul id='5C8kD'></ul><sub id='5C8kD'></sub></form><legend id='5C8kD'></legend><bdo id='5C8kD'><pre id='5C8kD'><center id='5C8kD'></center></pre></bdo></b><th id='5C8kD'></th></span></q></dt></tr></i><div id='5C8kD'><tfoot id='5C8kD'></tfoot><dl id='5C8kD'><fieldset id='5C8kD'></fieldset></dl></div>

      <bdo id='5C8kD'></bdo><ul id='5C8kD'></ul>
    1. <legend id='5C8kD'><style id='5C8kD'><dir id='5C8kD'><q id='5C8kD'></q></dir></style></legend>
      <tfoot id='5C8kD'></tfoot>

      如何使用 Yii 框架 2.0 上传多个文件

      时间:2023-10-16
      • <small id='bjn6N'></small><noframes id='bjn6N'>

            <bdo id='bjn6N'></bdo><ul id='bjn6N'></ul>

            <legend id='bjn6N'><style id='bjn6N'><dir id='bjn6N'><q id='bjn6N'></q></dir></style></legend>

                <i id='bjn6N'><tr id='bjn6N'><dt id='bjn6N'><q id='bjn6N'><span id='bjn6N'><b id='bjn6N'><form id='bjn6N'><ins id='bjn6N'></ins><ul id='bjn6N'></ul><sub id='bjn6N'></sub></form><legend id='bjn6N'></legend><bdo id='bjn6N'><pre id='bjn6N'><center id='bjn6N'></center></pre></bdo></b><th id='bjn6N'></th></span></q></dt></tr></i><div id='bjn6N'><tfoot id='bjn6N'></tfoot><dl id='bjn6N'><fieldset id='bjn6N'></fieldset></dl></div>
                <tfoot id='bjn6N'></tfoot>
                  <tbody id='bjn6N'></tbody>
                本文介绍了如何使用 Yii 框架 2.0 上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                使用 Yii 框架 2.0 我希望能够上传多个文件.遵循 Yii 2 文档,在 Upload Multiple Files 我有以下型号.

                class Newsletter extends yiidbActiveRecord {公共 $attachment_file;公共函数规则(){返回 [[['attachment_file'], 'file', 'maxFiles' =>5],];}公共函数上传(){如果 ($this->validate()) {foreach ($this->attachment_file as $file) {echo '

                ';打印_r($文件);echo '</pre>';}返回真;} 别的 {返回假;}}}

                以下是我的观点.

                <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?><?= $form->field($model, 'attachment_file[]')->fileInput(['multiple' => true,]) ?><按钮>提交<?php ActiveForm::end() ?>

                在我的控制器中,我有以下代码片段.

                if (Yii::$app->request->isPost) {$model->attachment_file = UploadedFile::getInstances($model, 'attachment_file');如果($模型->上传()){死();//文件上传成功返回;}}

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

                解决方案

                看看我的尝试:查看代码

                 ['enctype' => 'multipart/form-data']]) ?><?= $form->field($uploadForm, 'files[]')->fileInput(['multiple' => true]) ?><button class="btn btn-primary">上传</button><?php ActiveForm::end() ?>

                在控制器中

                 使用 yiiwebUploadedFile;使用 appmodelsMultipleUploadForm;使用 appmodelsProductImage;......函数 actionUploadImage() {$form = new MultipleUploadForm();如果 (Yii::$app->request->isPost) {$form->files = UploadedFile::getInstances($form, 'files');if ($form->files && $form->validate()) {foreach ($form->files as $file) {$image = new ProductImage();如果 ($image->save()) {$file->saveAs($image->getPath());}}}}返回 $this->render('uploadImage', ['上传表格' =>$形式,]);}

                MultipleUploadForm 模型

                使用 yiiaseModel;使用 yiiwebUploadedFile;类 MultipleUploadForm 扩展模型{/*** @var UploadedFile[] 文件上传*/公共 $ 文件;/*** @return 数组验证规则.*/公共函数规则(){返回 [[['文件'], '文件', '扩展名' =>'jpg', 'mimeTypes' =>'图像/jpeg', 'maxFiles' =>10, 'skipOnEmpty' =>错误的],];}}

                此代码对我有用.希望这也适用于您.

                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 yiidbActiveRecord {
                    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 yiiwidgetsActiveForm; ?>
                
                <?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 yiiwebUploadedFile;
                    use appmodelsMultipleUploadForm;
                    use appmodelsProductImage;
                    .......
                    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 yiiaseModel;
                use yiiwebUploadedFile;
                
                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 上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 Yii2 中使用没有命名空间的类 下一篇:让 Gii 在 Yii 2.0 上工作

                相关文章

              • <i id='NQh4r'><tr id='NQh4r'><dt id='NQh4r'><q id='NQh4r'><span id='NQh4r'><b id='NQh4r'><form id='NQh4r'><ins id='NQh4r'></ins><ul id='NQh4r'></ul><sub id='NQh4r'></sub></form><legend id='NQh4r'></legend><bdo id='NQh4r'><pre id='NQh4r'><center id='NQh4r'></center></pre></bdo></b><th id='NQh4r'></th></span></q></dt></tr></i><div id='NQh4r'><tfoot id='NQh4r'></tfoot><dl id='NQh4r'><fieldset id='NQh4r'></fieldset></dl></div>
                  <legend id='NQh4r'><style id='NQh4r'><dir id='NQh4r'><q id='NQh4r'></q></dir></style></legend>
                  <tfoot id='NQh4r'></tfoot>
                1. <small id='NQh4r'></small><noframes id='NQh4r'>

                    <bdo id='NQh4r'></bdo><ul id='NQh4r'></ul>