Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/razonyang/yii2-uploader

Yii2 File Uploader
https://github.com/razonyang/yii2-uploader

Last synced: 20 days ago
JSON representation

Yii2 File Uploader

Awesome Lists containing this project

README

        

Yii2 Uploader
=============

[![Build Status](https://travis-ci.org/razonyang/yii2-uploader.svg?branch=master)](https://travis-ci.org/razonyang/yii2-uploader)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/razonyang/yii2-uploader/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/razonyang/yii2-uploader/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/razonyang/yii2-uploader/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/razonyang/yii2-uploader/?branch=master)
[![Latest Stable Version](https://img.shields.io/packagist/v/razonyang/yii2-uploader.svg)](https://packagist.org/packages/razonyang/yii2-uploader)
[![Total Downloads](https://img.shields.io/packagist/dt/razonyang/yii2-uploader.svg)](https://packagist.org/packages/razonyang/yii2-uploader)
[![LICENSE](https://img.shields.io/github/license/razonyang/yii2-uploader)](LICENSE)

Supports multiple filesystems:

- [Yii2 Flysystem](https://github.com/creocoder/yii2-flysystem) - Local, FTP, AWS S3, Azure filesystem etc.
- [Aliyun OSS Flysystem](https://github.com/razonyang/yii2-flysystem-aliyun-oss)

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

```
composer require razonyang/yii2-uploader
```

Usage
-----

Configuration:

```php
return [
'components' => [
'filesystem' => [
'class' => \creocoder\flysystem\LocalFilesystem::class,
'path' => '@webroot/resources',
],
'uploader' => [
'class' => \RazonYang\Yii2\Uploader\Uploader::class,
'host' => 'http://localhost/resources', // the hostname relative to your uploaded files
'filesystem' => 'filesystem',
],
],
];
```

And then defines a form, `UploadForm`

```php
class UploadForm extends \yii\base\Model
{
use \RazonYang\Yii2\Uploader\UploadModelTrait;

public function handle()
{
if (!$this->validate()) {
// handles error
throw new \Exception('invalid file');
}

$url = $this->upload();
return [
'url' => $url,
// ... other information
];
}
}

class UploadController extends \yii\web\Controller
{
public function actionUpload()
{
$form = new UploadForm([
'file' => \yii\web\UploadedFile::getInstanceByName('file')
]);
return $form->handle();
}
}
```