2017 © Pedro Peláez
 

yii2-extension yii2-attachments

Extension for file uploading and attaching to the models

image

axelpal/yii2-attachments

Extension for file uploading and attaching to the models

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 50 Forks
  • 1 Open issues
  • 3 Versions
  • 9 % Grown

The README.md

Yii2 attachments

Extension for file uploading and attaching to the models, (*1)

This fork has been made by me and by company Elitmaster., (*2)

Demo

You can see the demo on the krajee website, (*3)

Installation

  1. The preferred way to install this extension is through composer., (*4)

    Either run, (*5)

    composer require axelpal/yii2-attachments "dev-master"
    

    or add, (*6)

    "axelpal/yii2-attachments": "dev-master"
    

    to the require section of your composer.json file., (*7)

  2. Add module to your main config:, (*8)

    'aliases' => [
            '@file' => dirname(__DIR__),
        ],
        'modules' => [
            'file' => [
                'class' => 'file\FileModule',
                'webDir' => 'files',
                'tempPath' => '@common/uploads/temp',
                'storePath' => '@common/uploads/store',
                'rules' => [ // Правила для FileValidator
                    'maxFiles' => 20,
                    'maxSize' => 1024 * 1024 * 20 // 20 MB
                ],
            ],
        ],
    

    Also, add these lines to your console config:, (*9)

    'controllerMap' => [
            'file' => [
                'class' => 'yii\console\controllers\MigrateController',
                'migrationPath' => '@file/migrations'
            ],
        ],
    
  3. Apply migrations, (*10)

    php yii migrate/up --migrationPath=@vendor/axelpal/yii2-attachments/migrations
    
  4. Attach behavior to your model (be sure that your model has "id" property), (*11)

    public function behaviors()
    {
        return [
            ...
            'fileBehavior' => [
                'class' => \file\behaviors\FileBehavior::className()
            ]
            ...
        ];
    }
    
  5. Make sure that you have added 'enctype' => 'multipart/form-data' to the ActiveForm options, (*12)

  6. Make sure that you specified maxFiles in module rules and maxFileCount on AttachmentsInput to the number that you want, (*13)

Usage

  1. In the form.php of your model add file input, (*14)

    <?= \file\components\AttachmentsInput::widget([
        'id' => 'file-input', // Optional
        'model' => $model,
        'options' => [ // Options of the Kartik's FileInput widget
            'multiple' => true, // If you want to allow multiple upload, default to false
        ],
        'pluginOptions' => [ // Plugin options of the Kartik's FileInput widget 
            'maxFileCount' => 10 // Client max files
        ]
    ]) ?>
    
  2. Use widget to show all attachments of the model in the view.php, (*15)

    <?= \file\components\AttachmentsTable::widget(['model' => $model]) ?>
    
  3. (Deprecated) Add onclick action to your submit button that uploads all files before submitting form, (*16)

    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', [
        'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary',
        'onclick' => "$('#file-input').fileinput('upload');"
    ]) ?>
    
  4. You can get all attached files by calling $model->files, for example:, (*17)

    foreach ($model->files as $file) {
        echo $file->path;
    }
    

The Versions

12/04 2016

dev-master

9999999-dev

Extension for file uploading and attaching to the models

  Sources   Download

BSD-4-Clause

The Requires

 

by Alexander Palchikov aka AxelPAL

file extension yii2 upload attachment

12/04 2016

v1.0.1

1.0.1.0

Extension for file uploading and attaching to the models

  Sources   Download

BSD-4-Clause

The Requires

 

by Alexander Palchikov aka AxelPAL

file extension yii2 upload attachment

16/11 2015

v1.0

1.0.0.0

Extension for file uploading and attaching to the models

  Sources   Download

BSD-4-Clause

The Requires

 

by Alexander Palchikov aka AxelPAL

file extension yii2 upload attachment