PHP code example of huijiewei / yii2-upload

1. Go to this page and download the library: Download huijiewei/yii2-upload library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

huijiewei / yii2-upload example snippets


    // 在 Yii2 components 配置里面增加
    
    'upload' => [
        'class' => \huijiewei\upload\drivers\LocalFile::class,
        'path' => '文件上传的目录',
        'uploadAction' => 'site/upload-file',
        'cropAction' => 'site/crop-image'
    ]
    
    // 根据上面的 action 配置,在对应的控制器里面增加
    
    public function actions()
    {
        return [
            'upload-file' => [
                'class' => \huijiewei\upload\UploadAction::class,
            ],
            'crop-image' => [
                'class' => \huijiewei\upload\ImageCropAction::class,
            ]
        ];
    }
    
    // 使用 widgets 命名空间下的 widget
    
    \huijiewei\upload\widgets\FileUploadWidget::widget([
        'name' => 'file',
        'value' => '',
    ]);
    
    \huijiewei\upload\widgets\ImageUploadWidget::widget([
        'name' => 'image',
        'value' => '',
    ]);
    
    
    // 支持多文件
    
    \huijiewei\upload\widgets\FileUploadWidget::widget([
        'name' => 'files[]',
        'value' => [],
        'multiple' => true
    ]);
    
    // 云存储支持图片样式
    
    \huijiewei\upload\widgets\ImageUploadWidget::widget([
        'name' => 'image',
        'value' => '',
        'imageStyleName' => '200x200' // 因为需要和本地切图兼容所以只支持 WxH 格式的样式名称
    ]);


    // 本地存储支持图片切割功能
   
    \huijiewei\upload\widgets\ImageUploadWidget::widget([
        'name' => 'image',
        'value' => '',
        'cropImage' => [
            'ratio' => 1,
            'size' => [100,100]
        ] 
    ]);

    // 阿里云 OSS
    
    'upload' => [
        'class' => \huijiewei\upload\drivers\AliyunOSS::class,
        'accessKeyId' => '',
        'accessKeySecret' => '',
        'endpoint' => '',
        'bucket' => '',
        'folder' => ''
    ],
    
    // 腾讯云 COS
    
     'upload' => [
        'class' => \huijiewei\upload\drivers\TencentCOS::class,
        'appId' => '',
        'secretId' => '',
        'secretKey' => '',
        'bucket' => '',
        'region' => '',
        'folder' => '',
    ],
    
    // 七牛
    
     'upload' => [
        'class' => \huijiewei\upload\drivers\Qiniu::class,
        'accessKey' => '',
        'secretKey' => '',
        'bucket' => '',
        'folder' => '',
        'uploadHost' => '',
        'bucketHost' => '',
        'bucketHttps' => false,
    ],