Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

php - How to upload multiple files using Yii framework 2.0

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 Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...