PHPackages                             outoffspace/yii2-attachments - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. outoffspace/yii2-attachments

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

outoffspace/yii2-attachments
============================

Extension for file uploading and attaching to the models

1.0.0-beta.3(9y ago)02BSD-4-ClausePHPPHP &gt;=5.4.0

Since Feb 4Pushed 5y agoCompare

[ Source](https://github.com/outOFFspace/yii2-attachments)[ Packagist](https://packagist.org/packages/outoffspace/yii2-attachments)[ Docs](https://github.com/Nemmo/yii2-attachments)[ RSS](/packages/outoffspace-yii2-attachments/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (5)Used By (0)

Yii2 attachments
================

[](#yii2-attachments)

[![Latest Stable Version](https://camo.githubusercontent.com/202174afa1e26a3a59049d8797c3e140970e24af061cce10ca04f08d92c266f4/68747470733a2f2f706f7365722e707567782e6f72672f6e656d6d6f2f796969322d6174746163686d656e74732f762f737461626c65)](https://packagist.org/packages/nemmo/yii2-attachments)[![License](https://camo.githubusercontent.com/516ae7fd6c5d421117029feac41be12acce5712ae58fc16f77a02163657e65ac/68747470733a2f2f706f7365722e707567782e6f72672f6e656d6d6f2f796969322d6174746163686d656e74732f6c6963656e7365)](https://packagist.org/packages/nemmo/yii2-attachments)[![Build Status](https://camo.githubusercontent.com/86683afec6d560f04507e5e2a4aa1743f897930f7d3057f8a4e4958bf1e71e12/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4e656d6d6f2f796969322d6174746163686d656e74732f6261646765732f6275696c642e706e673f623d7465737473)](https://scrutinizer-ci.com/g/Nemmo/yii2-attachments/build-status/tests)[![Code Coverage](https://camo.githubusercontent.com/d27e4ee1c2be8289a4d25371478d9e056a3d871774c132d7e3dafa7a74c419b0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4e656d6d6f2f796969322d6174746163686d656e74732f6261646765732f636f7665726167652e706e673f623d7465737473)](https://scrutinizer-ci.com/g/Nemmo/yii2-attachments/?branch=tests)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/eedaab195ea35e5b0e4f690c59a9444a5ecec8d84e30a290f875b86e32c83eb4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4e656d6d6f2f796969322d6174746163686d656e74732f6261646765732f7175616c6974792d73636f72652e706e673f623d7465737473)](https://scrutinizer-ci.com/g/Nemmo/yii2-attachments/?branch=tests)[![Total Downloads](https://camo.githubusercontent.com/975b28b66c10a012dac8bd02af4577d19c2ca958cc872292a97cdc20eedcc90d/68747470733a2f2f706f7365722e707567782e6f72672f6e656d6d6f2f796969322d6174746163686d656e74732f646f776e6c6f616473)](https://packagist.org/packages/nemmo/yii2-attachments)

Extension for file uploading and attaching to the models

Demo
----

[](#demo)

You can see the demo on the [krajee](http://plugins.krajee.com/file-input/demo) website

Installation
------------

[](#installation)

1. The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

    Either run

    ```
    php composer.phar require outoffspace/yii2-attachments "~1.0.0"

    ```

    or add

    ```
    "outoffspace/yii2-attachments": "~1.0.0"

    ```

    to the require section of your `composer.json` file.
2. Add module to `common/config/main.php`

    ```
    'modules' => [
    	...
    	'attachments' => [
    		'class' => outoffspace\attachments\Module::className(),
    		'tempPath' => '@app/uploads/temp',
    		'storePath' => '@app/uploads/store',
    		'rules' => [ // Rules according to the FileValidator
    		    'maxFiles' => 10, // Allow to upload maximum 3 files, default to 3
    		    'mimeTypes' => 'image/png', // Only png images
    		    'maxSize' => 1024 * 1024 // 1 MB
    		],
    		'tableName' => '{{%attachments}}' // Optional, default to 'attach_file'
    	]
    	...
    ]
    ```
3. Apply migrations

    ```
    	'controllerMap' => [
    	...
    	'migrate' => [
    		'class' => 'yii\console\controllers\MigrateController',
    		'migrationNamespaces' => [
    		    'outoffspace\attachments\migrations',
    		],
    	],
    	...
    	],
    ```

    ```
    php yii migrate/up

    ```
4. Attach behavior to your model (be sure that your model has "id" property)

    ```
    public function behaviors()
    {
    	return [
    		...
    		'fileBehavior' => [
    		    'class' => \outoffspace\attachments\behaviors\FileBehavior::className()
    		]
    		...
    	];
    }
    ```
5. Make sure that you have added `'enctype' => 'multipart/form-data'` to the ActiveForm options
6. Make sure that you specified `maxFiles` in module rules and `maxFileCount` on `AttachmentsInput` to the number that you want

Usage
-----

[](#usage)

1. In the `form.php` of your model add file input

    ```

    ```
2. Use widget to show all attachments of the model in the `view.php`

    ```

    ```
3. (Deprecated) Add onclick action to your submit button that uploads all files before submitting form

    ```
