PHPackages                             herroffizier/yii2-upload-manager - 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. herroffizier/yii2-upload-manager

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

herroffizier/yii2-upload-manager
================================

Simplifies file storing in Yii 2.

1.0.0(10y ago)12.1kPHP

Since Oct 26Pushed 10y ago1 watchersCompare

[ Source](https://github.com/herroffizier/yii2-upload-manager)[ Packagist](https://packagist.org/packages/herroffizier/yii2-upload-manager)[ RSS](/packages/herroffizier-yii2-upload-manager/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (8)Versions (2)Used By (0)

Yii2 Upload Manager
===================

[](#yii2-upload-manager)

[![Build Status](https://camo.githubusercontent.com/23c1eb4acd19c47e28737f7c056ec6a953f3a4d94bf0ae833ec59946b0b37b7c/68747470733a2f2f7472617669732d63692e6f72672f686572726f6666697a6965722f796969322d75706c6f61642d6d616e616765722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/herroffizier/yii2-upload-manager) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/d8e4116e4a39fddf068d5b4d88224df769956609f6bdfc8ea9c362e5e98b6747/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f686572726f6666697a6965722f796969322d75706c6f61642d6d616e616765722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/herroffizier/yii2-upload-manager/?branch=master) [![Code Coverage](https://camo.githubusercontent.com/8d7b7c6a62b480fc966705d62b1b92b068cfc336b0ef55f63347234fc079fa0f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f686572726f6666697a6965722f796969322d75706c6f61642d6d616e616765722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/herroffizier/yii2-upload-manager/?branch=master)

Yii2 Upload Manager is a small extension that organizes file uploads and takes control over upload paths and urls.

Features
--------

[](#features)

- Groups uploads into folders and folder structures with any depth.
- Divides upload folders into subfolders to avoid storing many files in one folder.
- Fights against file name collisions.
- Uses transparent file name generation mechanism.
- 100% code coverage :-)

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

[](#installation)

Install extension with Composer:

```
composer require herroffizier/yii2-upload-manager:@stable
```

Add extension to your application config:

```
'components' => [

    // ...

    'uploads' => [
        'class' => 'herroffizier\yii2um\UploadManager',
        // path to upload folder
        'uploadDir' => '@webroot/upload',
        // url to upload filder
        'uploadUrl' => '@web/upload',
    ],

    // ...
]
```

There is no need to create upload folder manually. Extension will make it automatically.

Usage
-----

[](#usage)

### Storing files.

[](#storing-files)

Extension provides few ways to store files.

Simply copy file to upload folder:

```
$filePath =
    Yii::$app->uploads->saveFile(
        // upload group
        'useless-files',
        // upload file name
        'file.txt',
        // original file name
        '/tmp/somefile.txt'
    );
```

Move file to upload folder:

```
$filePath =
    Yii::$app->uploads->moveFile(
        // upload group
        'useless-files',
        // upload file name
        'file.txt',
        // original file name
        '/tmp/somefile.txt'
    );
```

Save raw data as file in upload folder:

```
$content = 'test';

$filePath =
    Yii::$app->uploads->saveContent(
        // upload group
        'useless-files',
        // upload file name
        'file.txt',
        // file content
        $content
    );
```

Save `\yii\web\UploadedFile` instance to uplaod foder:

```
$upload = \yii\web\UploadedFile::getInstance(/* ... */);

$filePath =
    Yii::$app->uploads->saveUpload(
        // upload group
        'useless-files',
        // \yii\web\UploadedFile instance
        $upload
    );
```

As you may notice, all methods described above return `$filePath` value which is relative path to uploaded file and may be considered as unique upload id.

It can be converted to absolute file by method `getAbsolutePath` or to absolute url by method `getUrl`:

```
// get absolute path
$absoluteFilePath = Yii::$app->uploads->getAbsolutePath($filePath);

// get url
$relativeUrl = Yii::$app->uploads->getUrl($filePath);
```

### Name collisions

[](#name-collisions)

By deafult if you'll try to save file that already exists extension will throw an exception. Such behavior is not always suitable and you definitely don't want to solve each collision manually. So you have two different strategies which solve collisions automatically.

First strategy is to overwrite existing file silently. Such approach may be suitable for saving user avatars, for example.

Second strategy is to add incremental index to file name in case of collision. This strategy may be applied when dealing with user file uploads and original file names are important.

Now let's find out how to apply these strategies.

Strategy (throw exception, overwrite, add index) is identified by constant defined in `\herroffizier\yii2um\UploadManager` class: `\herroffizier\yii2um\UploadManager::STRATEGY_KEEP`, `\herroffizier\yii2um\UploadManager::STRATEGY_OVERWRITE` and `\herroffizier\yii2um\UploadManager::STRATEGY_RENAME`.

Also both methods `saveContent` and `saveUpload` have optional last parameter named `$overwriteStrategy` to which one of constants may be passed. Default value for `$overwriteStrategy` is `\herroffizier\yii2um\UploadManager::STRATEGY_KEEP`.

To sum up, let's take a look at example. Following code will throw an exception:

```
Yii::$app->uploads->saveContent(
    'useless-files',
    'file.txt',
    'test 1'
);

Yii::$app->uploads->saveContent(
    'useless-files',
    'file.txt',
    'test 2'
);
```

Hovewer, this code will work correctly because we applied overwrite strategy:

```
Yii::$app->uploads->saveContent(
    'useless-files',
    'file.txt',
    'test 1'
);

Yii::$app->uploads->saveContent(
    'useless-files',
    'file.txt',
    'test 2',
    \herroffizier\yii2um\UploadManager::STRATEGY_OVERWRITE
);
```

Now `file.txt` contains `test 2` string.

Finally, let's apply rename strategy:

```
$filePath1 = Yii::$app->uploads->saveContent(
    'useless-files',
    'file.txt',
    'test 1'
);

$filePath2 = Yii::$app->uploads->saveContent(
    'useless-files',
    'file.txt',
    'test 2',
    \herroffizier\yii2um\UploadManager::STRATEGY_RENAME
);

echo "$filePath1, $filePath2";
```

This code will also complete correctly and output `useless-files/75/file.txt, useless-files/75/file-1.txt`. As you may see, second file has an index `1` at the end of its name.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~157 days

Total

2

Last Release

3700d ago

Major Versions

0.1 → 1.0.02016-03-31

### Community

Maintainers

![](https://www.gravatar.com/avatar/820232d3f5de11b08332a98ca93b025ab46d7e3563ddaec2ce6d042828718983?d=identicon)[herroffizier](/maintainers/herroffizier)

---

Top Contributors

[![korotin](https://avatars.githubusercontent.com/u/277992?v=4)](https://github.com/korotin "korotin (21 commits)")

---

Tags

uploadyii2extensionyiiyii 2

###  Code Quality

TestsCodeception

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/herroffizier-yii2-upload-manager/health.svg)

```
[![Health](https://phpackages.com/badges/herroffizier-yii2-upload-manager/health.svg)](https://phpackages.com/packages/herroffizier-yii2-upload-manager)
```

###  Alternatives

[sjaakp/yii2-illustrated-behavior

ActiveRecord Behavior with associated Widget for Yii2.

423.1k](/packages/sjaakp-yii2-illustrated-behavior)[floor12/yii2-module-files

Yii2 module to upload and manage files to your models.

1612.4k6](/packages/floor12-yii2-module-files)[vova07/yii2-fileapi-widget

The FileAPI widget for Yii2 framework.

4765.5k9](/packages/vova07-yii2-fileapi-widget)

PHPackages © 2026

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