PHPackages                             artkost/yii2-attachment - 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. artkost/yii2-attachment

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

artkost/yii2-attachment
=======================

Yii 2 files attachment

31141[1 PRs](https://github.com/artkost/yii2-attachment/pulls)1PHP

Since Aug 27Pushed 8y ago2 watchersCompare

[ Source](https://github.com/artkost/yii2-attachment)[ Packagist](https://packagist.org/packages/artkost/yii2-attachment)[ RSS](/packages/artkost-yii2-attachment/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (1)

Yii2 Attachments
================

[](#yii2-attachments)

[![Build Status](https://camo.githubusercontent.com/cf17e9ff14b7e0083aea5db3adeacb1c4bc19a83210acfe27339ecbaeecd637c/68747470733a2f2f7472617669732d63692e6f72672f6172746b6f73742f796969322d6174746163686d656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/artkost/yii2-attachment)

This component provide ability to attach and upload

All uploaded files by default stored in database and have `TEMPORARY` status.

When model with attachments save yourself, all files attached to the model change their status to permanent.

Cli Commands
============

[](#cli-commands)

`php yii attachment/manager/clear` clears all temporary files from system

How to use
==========

[](#how-to-use)

Configure `Manager` component

```
return [
    'components' => [
        'attachmentManager' => [
            'class' => 'artkost\attachment\Manager',
            'storageUrl' => '@web/storage',
            'storagePath' => '@webroot/storage',
            'attachmentFileTable' => '{{%attachment_file}}'
        ]
    ]
]
```

Create your own type of file

```
namespace app\modules\user\models;

use artkost\attachment\models\ImageFile;

class UserAvatarFile extends ImageFile
{
    const TYPE = 'userProfile';

    //subfolder of storgae folder
    public $path = 'user/profile';
}
```

Create model that have `attachment_id` field, and attach behavior to it

> ATTENTION: model can have only one instance of behavior

```
/**
 * Class Profile
 * @package app\modules\user\models
 * User profile model.
 *
 * @property integer $user_id User ID
 * @property string $name Name
 * @property string $surname Surname
 * @property int $avatar_id Avatar //our attachment_id
 * @property boolean $sex
 *
 * @property User $user User
 * @property UserAvatarFile $avatar avatar file
 */
class UserProfile extends ActiveRecord
{

    public static function tableName()
    {
        return '{{%user_profile}}';
    }

    public function behaviors()
    {
        return [
            'attachBehavior' => [
                'class' => AttachBehavior::className(),
                'models' => [
                    'avatar' => [
                        'class' => UserAvatarFile::className()
                    ]
                ]
            ]
        ];
    }

    public function getAvatar()
    {
        // simply helper method with predefined conditions
        return $this->hasOneAttachment('avatar', ['id' => 'avatar_id']);
    }
}
```

Currently supported only `FileAPI` upload, but you can add yours.

Add action into controller

```
namespace app\modules\user\controllers;

use artkost\attachmentFileAPI\actions\UploadAction as FileAPIUpload;
use app\modules\user\models\UserProfile;

/**
 * Profile controller for authenticated users.
 */
class ProfileController extends Controller
{

    public function actions()
    {
        return [
            'fileapi-upload' => [
                'class' => FileAPIUpload::className(),
                'modelClass' => UserProfile::className(),
                'attribute' => 'avatar'
                //'accessCheck' => function($action) {  }
            ]
        ];
    }

}
```

in action view file you can use widget for upload files

```
use artkost\attachmentFileAPI\widgets\File as FileAPIWidget;
?>
...
