badbreze/yii2-attachments

This package is abandoned and no longer maintained. The author suggests using the lispa/amos-attachments package instead.

Extension for file uploading and attaching to the models

Installs: 519

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 47

Type:yii2-extension

1.2.12 2017-02-21 15:41 UTC

This package is not auto-updated.

Last update: 2018-02-27 09:03:02 UTC


README

Latest Stable Version Total Downloads License

Extension for file uploading and attaching to the models

This fork has the aim to implement some missing features such as multiple fields and simplifying installation

Demo

You can see the demo on the krajee website

Installation

  1. The preferred way to install this extension is through composer.

Either run

composer require badbreze/yii2-attachments

or add

"badbreze/yii2-attachments": ">=1.2.0"

to the require section of your composer.json file.

  1. Add module to your main config:
<?php
'aliases' => [
    '@file' => dirname(__DIR__),
],
'modules' => [
    'file' => [
        'class' => 'file\FileModule',
        'webDir' => 'files',
        'tempPath' => '@common/uploads/temp',
        'storePath' => '@common/uploads/store',
        'tableName' => '{{%attach_file}}' // Optional, default to 'attach_file'
    ],
],

Also, add these lines to your console config:

<?php
'controllerMap' => [
    'file' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => '@file/migrations'
    ],
],
  1. Apply migrations
php yii migrate/up --migrationPath=@vendor/badbreze/yii2-attachments/src/migrations
  1. Attach behavior to your model (be sure that your model has "id" property)
<?php
use yii\helpers\ArrayHelper;

/**
 * Declare file fields
 */
public $my_field_multiple_files;
public $my_field_single_file;

/**
 * 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. Make sure that you have added 'enctype' => 'multipart/form-data' to the ActiveForm options

  2. Make sure that you specified maxFiles in module rules and maxFileCount on AttachmentsInput to the number that you want

  3. Youre ready to use, See How