PHPackages                             gomonkey/yii2-easy-uploader - 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. gomonkey/yii2-easy-uploader

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

gomonkey/yii2-easy-uploader
===========================

an easy way for make folders and upload images or file everywhere

1.0.1.x-dev(8y ago)153424MITPHP

Since Nov 2Pushed 5y ago3 watchersCompare

[ Source](https://github.com/gomonkey/yii2-easy-uploader)[ Packagist](https://packagist.org/packages/gomonkey/yii2-easy-uploader)[ RSS](/packages/gomonkey-yii2-easy-uploader/feed)WikiDiscussions master Synced 2mo ago

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

[![Yii2](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](http://www.yiiframework.com/)

Easy uploader extension for yii2
================================

[](#easy-uploader-extension-for-yii2)

an easy way for make folders and upload images with one simple code line.

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

[](#installation)

The preferred way to install this extension is through [composer](../../../../web/index.phpdownload/).

Either run

```
php composer.phar require  gomonkey/yii2-easy-uploader "*"

```

or add

```
"gomonkey/yii2-easy-uploader": "*"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

### Parameters

[](#parameters)

Add in your config file ( common/config/main.php ) for using in frontend and backend

```
'components' => [
        'uploaders' => [
            'class' => 'gomonkey\uploader\Uploader',
            'baseFrontendUrl' =>   dirname(dirname(__DIR__)) . '/frontend/web/images',
            'baseBackendUrl' => dirname(dirname(__DIR__)) . '/backend/web/images',
            'rename' => true, // Rename file
            'random' => 12, // random alphanumeric name
            'remove' => true, // Remove original file after upload
            'folders' => [
                [
                    'name' => '1200',
                    'quality' => 70,
                    'width' => 1200
                ],
                [
                    'name' => '800',
                    'quality' => 70,
                    'width' => 800
                ],
                [
                    'name' => '600',
                    'quality' => 70,
                    'width' => 600
                ],
                [
                    'name' => 'avatars',
                    'quality' => 70,
                    'width' => 200
                ]

            ]

        ],
    ],
```

if you use basic template, you can still use the same code above, just put the code in you config file and change baseFrontendUrl. You can remove or comment baseBackendUrl

### Controllers

[](#controllers)

add UploadFile in your controller

```
use yii\web\UploadedFile;
```

#### single image upload

[](#single-image-upload)

In your controller action :

```
$upload = new Yii::$app->uploaders();

/**
If you want to use backend path:
$upload = new Yii::$app->uploaders("backend");
**/

$model->image =  $upload->upload(UploadedFile::getInstance($model, 'image'), "avatars");
```

$model-&gt;image now have the name of the uploaded image, ready to save it in database.

#### multiple uploads

[](#multiple-uploads)

```
foreach (UploadedFile::getInstances($model, 'image') as $file) {
    $model->image = (new Yii::$app->uploaders())->upload($file, "avatars");

}
```

#### upload from url

[](#upload-from-url)

Now it is possible to upload images from other website, using dynamic or static urls. the same code can be used in a loop

```
 $model->image = $upload->uploadFromUrl("https://www.website.it/url/to/image/image.jpg",  "myFolder");
```

N.B. no need upload instance

#### infinite folders generation

[](#infinite-folders-generation)

You can make infinite folders. For example with user id: images/user/3/1200/imagename.jpg

```
$model->image =  $upload->upload(UploadedFile::getInstance($model, 'image'), "users/".Yii::$app->user->id);
```

#### in your view

[](#in-your-view)

do not forget to use multipart/form-data to your form

```
