amos-attachments
Extension for file uploading and attaching to the models
Demo
You can see the demo on the krajee website
Installation
- The preferred way to install this extension is through composer.
Either run
composer require open20/amos-attachments
or add
"open20/amos-attachments": ">=1.0"
to the require section of your composer.json
file.
- Add module to your main config in 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'
],
],
Also, add these lines to your console config:
<?php
'controllerMap' => [
'attachments' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => '@amos_attachments/migrations'
],
],
- Apply migrations
php yii migrate/up --migrationPath=@vendor/open20/amos-attachments/src/migrations
- Attach behavior to your model (be sure that your model has "id" property)
<?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'],
]);
}
-
Make sure that you have added
'enctype' => 'multipart/form-data'
to the ActiveForm options -
Make sure that you specified
maxFiles
in module rules andmaxFileCount
onAttachmentsInput
to the number that you want -
Youre ready to use, See How