Extension for file uploading and attaching to the models
This fork has been made by me and by company Elitmaster.
You can see the demo on the krajee website
-
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
-
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; }