PHPackages                             d3yii2/d3files - 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. d3yii2/d3files

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

d3yii2/d3files
==============

Extension for file uploading and attaching to the models

0.9.102(1y ago)21.0k2[3 issues](https://github.com/d3yii2/d3files/issues)[1 PRs](https://github.com/d3yii2/d3files/pulls)BSD-4-ClausePHPPHP &gt;=5.4.0CI passing

Since Feb 26Pushed 1y ago2 watchersCompare

[ Source](https://github.com/d3yii2/d3files)[ Packagist](https://packagist.org/packages/d3yii2/d3files)[ Docs](https://github.com/d3yii2/d3files)[ RSS](/packages/d3yii2-d3files/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (5)Versions (118)Used By (0)

d3files (inspired from d2files)
===============================

[](#d3files-inspired-from-d2files)

[![Yii2](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](https://www.yiiframework.com/)[![Latest Stable Version](https://camo.githubusercontent.com/0c16257b4039434c8f7aa51cae1d57abaf1a38badd39f3b607027e206ab09bbc/68747470733a2f2f706f7365722e707567782e6f72672f6433796969322f643366696c65732f762f737461626c65)](https://packagist.org/packages/d3yii2/d3files)[![Total Downloads](https://camo.githubusercontent.com/4ab4847e6bcb9a476f15b7fbb306d711bfad9bdaa3b53810def50f3b7132cc29/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6433796969322f643366696c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/d3yii2/d3files)[![Latest Unstable Version](https://camo.githubusercontent.com/b528ec8f5e914685b5a1b50b1c58200ae34dd590362f23cb0ad4a91e694ef10c/68747470733a2f2f706f7365722e707567782e6f72672f6433796969322f643366696c65732f762f756e737461626c65)](https://packagist.org/packages/d3yii2/d3files)[![Dependency Status](https://camo.githubusercontent.com/2ecdf5d33966c979054ddd4443c14ec74a22d5e53939134e921d28b2346861a0/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3538366134313465343962663262303034333764343262612f62616467652e7376673f7374796c653d666c61742d737175617265)](https://www.versioneye.com/user/projects/586a414e49bf2b00437d42ba)[![License](https://camo.githubusercontent.com/a9f4d58d0aecfcf1f7a2ac47edf36a13f40b81fbdb210d5f7cadaab44e9b1513/68747470733a2f2f706f7365722e707567782e6f72672f6433796969322f643366696c65732f6c6963656e7365)](https://packagist.org/packages/d3yii2/d3files)

Extension for file uploading and attaching to the models

Features
--------

[](#features)

- attach files to model record (it is possible to attach to one model multiple files)
- widget for model view
- access rights realised as standalone actions (separate download, upload, delete) by integrating in model's controllers
- shared files for public access

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

[](#installation)

```
php composer.phar require d3yii2/d3files dev-master
```

- add to config/web.php

```
    'modules' => [
        'd3files' => [
            'class'              => 'd3yii2\d3files\D3Files',
            'uploadDir'          => dirname(__DIR__) . '\upload\d3files',
            'disableController'  => false,  // set true to disable d3files controller to use model's controllers
            'hashSalt'           => false, // Set salt in your web-local.php config, empty value will disable sharing
            'sharedExpireDays'   => 5,
            'sharedLeftLoadings' => 5,
            'controllerRoute'    => 'delivery/attachments', //define controler route, where defined
        ],
    ],
```

- migration configuration. Add to console migration definition path

```
    'controllerMap' => [
           'migrate' => [
               'class' => 'yii\console\controllers\MigrateController',
               'migrationPath' => [
                   '@d3yii2/d3files/migrations',
               ],
           ],
```

- do migration

```
yii migrate
```

Usage
-----

[](#usage)

### Widget

[](#widget)

Allow upload, download, delete files for model record.

```

```

### D3FilesPreviewWidget

[](#d3filespreviewwidget)

Extends D3FilesWidget with file preview.

```

```

File types are determined automatically to load PDF or images inside modal dialog window.

Dependencies:

eaBlankonThema\\widget\\ThModal

d3system\\yii2\\web\\D3SystemView

Main layout should have footer code:

```

=======
### Widget

Allow upload, download, delete files for model record.

```php

```

### Access control

[](#access-control)

In config disableController set true for disabling use d3files controller, where no realised any access control.

```
    'modules' => [
        'd3files' => [
            ....
            'disableController'  => true,  // set true to disable d3files controller to use model's controllers
            .....
        ],
    ],
```

For implementing access control add separate actions for upload, download and delete to model controller. Can implement any standard Yii2 access control. For example RBAC.

```
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $addBehaviors = [
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'only' => ['d3filesdownload', 'd3filesupload', 'd3filesdelete'],
                'rules' => [
                    // deny all POST requests
                    [
                        'allow' => true,
                        'actions' => [
                            'd3filesdownload',
                            'd3filesopen',
                            'd3filesupload',
                            'd3filesdelete',
                            'd3fileseditnotes'
                        ],
                        'roles' => ['role1','role2'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => \yii\filters\VerbFilter::className(),
                'actions' => [
                    'd3filedelete' => ['POST'],
                    'd3fileupload' => ['POST'],
                ],
            ],
        ];
        return array_merge(parent::behaviors(), $addBehaviors);
    }

    public function actions() {
        return [
            'd3filesdownload' => [
                'class' => d3yii2\d3files\components\DownloadAction::class,
                'modelName' => D3pPerson::class,
            ],
            'd3filesupload'   => [
                'class' => d3yii2\d3files\components\UploadAction::class,
                'modelName' => D3pPerson::class,
            ],
            'd3filesdelete'   => [
                'class' => \d3yii2\d3files\components\DeleteAction::class,
                'modelName' => D3pPerson::class,
            ],
            // for D3FilesPreviewWidget
            'd3filesopen' => [
                'class' => Dd3yii2\d3files\components\ownloadAction::class,
                'modelName' => D3pPerson::class,
                'downloadType' => 'open'
            ],
            // for D3FilesPreviewWidget
            'd3filesopen' => [
                'class' => 'd3yii2\d3files\components\DownloadAction',
                'modelName' => RkInvoice::class,
                'downloadType' => 'open'
            ],
            // for D3FilesPreviewWidget
            'd3fileseditnotes' => [
                'class' => EditNotesAction::class,
                'modelName' => D3pPerson::class,
            ],
        ];
    }
```

Widget

```
d3yii2\d3files\widgets\D3FilesWidget::widget(
    [
        'model' => $model,
        'model_id' => $model->id,
        'title' => 'Attachments',
        'icon' => false,
        'hideTitle' => false,
        'readOnly' => false
    ]
)
```

Preview widget (load in modal dialog window)

```
d3yii2\d3files\widgets\D3FilesPreviewWidget::widget([
    'model' => $model,           // Model to load attachment(s) from
    'fileList' => [...]          // Resulting array of the ModelD3Files::fileListForWidget if the attachments are joined already. Required if model not specified.
    'defaultExtension' => 'pdf', // Optional (PDF by default),
    'viewExtensions' => ['pdf']  // Optional.- (['pdf', 'png', 'jpg', 'jpeg'] by default)
])
```

For the PDF files server should support iframes. If the files are not loading check for the x-frame-options in the respose headers. In case:

```
x-frame-options: deny
```

You can set set in webserver configuration:

```
X-Frame-Options:SAMEORIGIN
```

Or one of the directives in .htaccess

```
X-Frame-Options: sameorigin
```

```
X-Frame-Options: "allow-from https://example.com/"
```

### Active Form

[](#active-form)

- to Active form model add property for uploading file

```
public $uploadFile;
```

- to Active model for new attribute add rule

```
    public function rules() {
        return [
            ......,
            [
                ['uploadFile'],
                'file',
                'skipOnEmpty' => true,
                'extensions' => 'png, jpg, pdf, xls, doc'
            ],
        ];
    }
```

- in controller add use

```
use d3yii2\d3files\models\D3files;
```

- in controller action after successful save() add

```
$model->uploadFile = UploadedFile::getInstance($model, 'uploadFile');
D3files::saveYii2UploadFile($model->uploadFile, ModelName::className(), $model->id);
```

- in form to Active form set 'enctype' = 'multipart/form-data',

```
$form = ActiveForm::begin([
                'id' => 'xxxxxxx',
                'layout' => 'horizontal',
                'enableClientValidation' => true,
                'options' => [
                    'enctype' => 'multipart/form-data',
                    ],
                ]
    );
```

- in form view add upload field

```
echo $form->field($model, 'uploadFile')->fileInput();
```

### Shared (public) access

[](#shared-public-access)

- to create share implement share generation request in your code:

```
//$id is D3filesModel model's ID
$share = D3filesModel::createSharedModel($id, $expireDays, $leftLoadings);
$shared_id   = $share['id'];
$shared_hash = $share['hash'];
```

- and use those variables to create url:

```
$url = 'http://www.yoursite.com/index.php?r=d3files/d3files/downloadshare&id=' . $shared_id . '&hash=' . $shared_hash;
echo $url;
```

### Get record files list

[](#get-record-files-list)

```
use d3yii2\d3files\models\D3files;
$filesList = D3files::getRecordFilesList($model::className(),$model->id)
```

### Get record files with full path

[](#get-record-files-with-full-path)

```
    public static function getRecordFiles(string $className, int $recordId): array
    {
        $files = [];
        foreach (D3Files::getModelFilesList($className, $recordId, true) as $file) {
            $fileHandler = new FileHandler([
                'model_name' => $file['className'],
                'model_id' => $file['id'],
                'file_name' => $file['file_name']
            ]);
                $files[] = [
                    'fileName' => $file['file_name'],
                    'filePath' => $fileHandler->getFilePath(),
                ];
        }
        return $files;
    }
```

### Attach existing file to record

[](#attach-existing-file-to-record)

```
$fileTypes = '/(gif|pdf|dat|jpe?g|png|doc|docx|xls|xlsx|htm|txt|log|mxl|xml|zip)$/i';
$model = Users::findOne($id);
$fileName = 'MyAvatar.jpg';
$filePath = '/temp/avatar.jpg';
D3files::saveFile($fileName, Users::className(), $model->id, $filePath, $fileTypes);
```

### Attach existing content to record as attachment

[](#attach-existing-content-to-record-as-attachment)

```
        D3files::saveContent(
            'import-' . date('YmdHis') . '.csv',
            get_class($voyage),
            $voyage->id,
            $this->csv,
            '/(csv|txt)$/i',
            Yii::$app->user->id
        );
```

### Attach already saved file to record

[](#attach-already-saved-file-to-record)

```
$model = Users::findOne($id);
D3filesModel::createCopy($fileModelId, Users::class, $model->id);
```

### Maintenance commands

[](#maintenance-commands)

Soft deletes files (sets deleted=1) For model dektrium\\user\\models\\User soft delete from database for model dektrium\\user\\models\\User oldest as 12 months files with extension xml

```
yii d3files/clean-files/remove-older-than 'dektrium\user\models\User' 12 '%.xml'
```

Deletes files and corresponding records in database which have deleted=1

```
yii d3files/clean-files/remove-files 'dektrium\user\models\User'
```

Deletes model files from filesystem with out refence in database

```
yii d3files/clean-files/unused-files  'poker\poker\models\PkPlaygroundFixes'
```

### Change log

[](#change-log)

- 0.9.0 (Feb 26, 2017) - added RU translation
- 0.9.3 (May 29, 2017) - auto creating upload directories
- 0.9.4 (Nov 16, 2017) - added parameter controllerRoute
- 0.9.13 (Jul 2, 2018) - added action column
- 0.9.93 (febr 10, 2022) - added controller d3files/clean-files for maintenance
- 0.9.97 (jun 28, 2022) - improved maintanance controller d3files/clean-files

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~30 days

Recently: every ~103 days

Total

101

Last Release

369d ago

Major Versions

0.9.27 → 20191013.x-dev2019-10-13

### Community

Maintainers

![](https://www.gravatar.com/avatar/542187ba859514d10d0952dca77df8ea889a9651b249d0b5b513da791fd2919b?d=identicon)[uldisn](/maintainers/uldisn)

---

Top Contributors

[![anothersoftware-lv](https://avatars.githubusercontent.com/u/9327511?v=4)](https://github.com/anothersoftware-lv "anothersoftware-lv (146 commits)")[![uldisn](https://avatars.githubusercontent.com/u/3525344?v=4)](https://github.com/uldisn "uldisn (134 commits)")[![ivarsju](https://avatars.githubusercontent.com/u/4513623?v=4)](https://github.com/ivarsju "ivarsju (26 commits)")[![viljums](https://avatars.githubusercontent.com/u/6377908?v=4)](https://github.com/viljums "viljums (7 commits)")[![VairisO](https://avatars.githubusercontent.com/u/9198246?v=4)](https://github.com/VairisO "VairisO (3 commits)")[![rokorolov](https://avatars.githubusercontent.com/u/6349248?v=4)](https://github.com/rokorolov "rokorolov (1 commits)")

---

Tags

filesyii2widgetattachmentsuploader

### Embed Badge

![Health badge](/badges/d3yii2-d3files/health.svg)

```
[![Health](https://phpackages.com/badges/d3yii2-d3files/health.svg)](https://phpackages.com/packages/d3yii2-d3files)
```

###  Alternatives

[vova07/yii2-fileapi-widget

The FileAPI widget for Yii2 framework.

4765.5k9](/packages/vova07-yii2-fileapi-widget)[noam148/yii2-image-manager

A Yii2 module/widget for upload and cropping images

12914.8k](/packages/noam148-yii2-image-manager)[limion/yii2-jquery-fileupload-widget

Blueimp file upload widget for Yii2

1224.5k](/packages/limion-yii2-jquery-fileupload-widget)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
