yii2-extension yii2-attachments
Extension for file uploading and attaching to the models
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
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
-
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)
-
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'
],
],
-
Apply migrations, (*10)
php yii migrate/up --migrationPath=@vendor/axelpal/yii2-attachments/migrations
-
Attach behavior to your model (be sure that your model has "id" property), (*11)
public function behaviors()
{
return [
...
'fileBehavior' => [
'class' => \file\behaviors\FileBehavior::className()
]
...
];
}
-
Make sure that you have added 'enctype' => 'multipart/form-data'
to the ActiveForm options, (*12)
-
Make sure that you specified maxFiles
in module rules and maxFileCount
on AttachmentsInput
to the number that you want, (*13)
Usage
-
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
]
]) ?>
-
Use widget to show all attachments of the model in the view.php
, (*15)
<?= \file\components\AttachmentsTable::widget(['model' => $model]) ?>
-
(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');"
]) ?>
-
You can get all attached files by calling $model->files
, for example:, (*17)
foreach ($model->files as $file) {
echo $file->path;
}
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
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
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