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

2022-01-07 00:00:00 file-upload php yii2

使用 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.

相关文章