andrewljashenko/yii2-upload-files-behavior

通过行为上传文件

dev-master 2015-12-22 12:44 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:04:00 UTC


README

通过行为上传文件

安装

安装此扩展的首选方式是通过 composer.

运行以下命令之一

php composer.phar require --prefer-dist andrewljashenko/yii2-upload-files-behavior "*"

或者在您的 composer.json 文件的 require 部分添加

"andrewljashenko/yii2-upload-files-behavior": "*"

到您的 composer.json 文件中。

使用方法

扩展安装完成后,只需在您的代码中使用它即可

[
    'class' => UploadFilesBehavior::className(), //Behavior class
    'attributes' => [
        [
            'attribute' => 'images',
            'uploadPath' => '@common/images',
            'sizes' => [[100, 100], [200, 250]]
        ],
        [
            'attribute' => 'file',
            'uploadPath' => '@common/files',
        ],
    ]
]

别忘了在 ActiveForm 中启用 enctype

<?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]); ?>
    <?= $form->field($model, 'images[]')->fileInput(['multiple' => true]) ?>
    <?= $form->field($model, 'file')->fileInput() ?>
<?php ActiveForm::end(); ?>