open20/amos-attachments

该包最新版本(1.15.1)没有可用的许可信息。

文件上传和附加到模型的扩展


README

文件上传和附加到模型的扩展

演示

您可以在 krajee 网站上查看演示

安装

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

运行以下命令之一

composer require open20/amos-attachments

或者

"open20/amos-attachments": ">=1.0"

将以下内容添加到您的 composer.json 文件的 require 部分。

  1. 将模块添加到主配置中的 common 部分
<?php
'aliases' => [
    '@file' => dirname(__DIR__),
],
'modules' => [
    'attachments' => [
        'class' => 'open20\amos\attachments\FileModule',
        'webDir' => 'files',
        'tempPath' => '@common/uploads/temp',
        'storePath' => '@common/uploads/store',
        // 'tableName' => '{{%attach_file}}' // Optional, default to 'attach_file'
    ],
],

同时,将以下行添加到您的控制台配置中

<?php
'controllerMap' => [
    'attachments' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => '@amos_attachments/migrations'
    ],
],
  1. 应用迁移
php yii migrate/up --migrationPath=@vendor/open20/amos-attachments/src/migrations
  1. 将行为附加到您的模型(请确保您的模型有 "id" 属性)
<?php
use yii\helpers\ArrayHelper;

/**
 * Adding the file behavior
 */
public function behaviors()
{
    return ArrayHelper::merge(parent::behaviors(), [
        'fileBehavior' => [
            'class' => \file\behaviors\FileBehavior::className()
        ]
    ]);
}

/**
 * Add the new fields to the file behavior
 */
public function rules()
{
    return ArrayHelper::merge(parent::rules(), [
        [['my_field_multiple_files', 'my_field_single_file'], 'file'],
    ]);
}
  1. 确保您已将 'enctype' => 'multipart/form-data' 添加到 ActiveForm 选项中

  2. 确保您在模块规则中指定了 maxFiles,并在 AttachmentsInput 中指定了 maxFileCount 到您想要的数量

  3. 您现在可以使用它了,查看如何使用