axelpal / yii2-attachments
Extension for file uploading and attaching to the models
Installs: 683
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 57
Type:yii2-extension
Requires
- php: >=5.4.0
- himiklab/yii2-colorbox-widget: *
- kartik-v/yii2-widget-fileinput: v1.0.3
- yiisoft/yii2: *
- yiisoft/yii2-jui: ^2.0
This package is auto-updated.
Last update: 2024-11-05 00:53:55 UTC
README
Extension for file uploading and attaching to the models
This fork has been made by me and by company Elitmaster.
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 axelpal/yii2-attachments "dev-master"
or add
"axelpal/yii2-attachments": "dev-master"
to the require section of your
composer.json
file. -
Add module to your main config:
'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:
'controllerMap' => [ 'file' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => '@file/migrations' ], ],
-
Apply migrations
php yii migrate/up --migrationPath=@vendor/axelpal/yii2-attachments/migrations
-
Attach behavior to your model (be sure that your model has "id" property)
public function behaviors() { return [ ... 'fileBehavior' => [ 'class' => \file\behaviors\FileBehavior::className() ] ... ]; }
-
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
Usage
-
In the
form.php
of your model add file input<?= \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 ] ]) ?>
-
Use widget to show all attachments of the model in the
view.php
<?= \file\components\AttachmentsTable::widget(['model' => $model]) ?>
-
(Deprecated) Add onclick action to your submit button that uploads all files before submitting form
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'onclick' => "$('#file-input').fileinput('upload');" ]) ?>
-
You can get all attached files by calling
$model->files
, for example:foreach ($model->files as $file) { echo $file->path; }