razonyang/yii2-uploader

Yii2 Uploader

安装次数: 3,989

依赖: 1

建议者: 0

安全性: 0

星标: 6

关注者: 3

分支: 0

开放问题: 1

类型:yii2-extension

1.0.1 2019-08-30 10:39 UTC

This package is auto-updated.

Last update: 2024-08-29 05:18:06 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads LICENSE

支持多个文件系统

安装

composer require razonyang/yii2-uploader

使用

配置

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

然后定义一个表单,UploadForm

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();
    }
}