# 文件上传

# 前言

herosphp/storage (opens new window) 基于适配器机制提供统一的API方式,动态扩展,支持多种文件驱动上传。已实现Local、QiNiu、Minio。

# 安装

# 1. 安装扩展

composer require herosphp/storage

# 2. 生成配置文件storage.config.php

composer vendor:publish "herosphp/storage"

配置文件

<?php

declare(strict_types=1);

use herosphp\plugin\storage\handler\LocalFileHandler;
use herosphp\plugin\storage\handler\MinioFileSaveHandler;
use herosphp\plugin\storage\handler\QiniuFileSaveHandler;

return [
    // Allowed file extesion
    'allow_ext' => 'jpg|jpeg|png|gif|txt|pdf|rar|zip|swf|bmp|c|java|mp3',
    // Rejected file extension
    'reject_ext' => 'exe|sh|bat',
    // Allowed max file size, default value is  5MiB,
    // if no limits, set it to 0, default: 5MiB
    'max_size' => 5242880,

    'default_handler' => 'local',
    'handlers' => [
        'local' => [
            'class' => LocalFileHandler::class,
            'config' => [
                'root' => PUBLIC_PATH . 'upload',
                'url' => 'http://127.0.0.1:2345/upload/'
            ]
        ],

        'qiniu' => [
            'class' => QiniuFileSaveHandler::class,
            'config' => [
                'access_key' => 'QINIU_ACCESS_KEY',
                'accessKey' => 'QINIU_ACCESS_KEY',
                'secretKey' => 'QINIU_SECRET_KEY',
                'bucket' => 'QINIU_BUCKET',
                'domain' => 'QINBIU_DOMAIN',
            ]
        ],

        'minio' => [
            'class' => MinioFileSaveHandler::class,
            'config' => [
                'credentials' => [
                    'key' => 'key',
                    'secret' => 'secret',
                ],
                'region' => '',
                'version' => 'latest',
                'bucket_endpoint' => false,
                'use_path_style_endpoint' => true,
                'endpoint' => 'http://172.28.1.51:9000',
                'bucket_name' => 'aas',
                'domain' => 'http://172.28.1.51:9000',
                'is_set_policy' => true, //only true,set policies.
                'policies' => [  //options
                    'read+write' => ['*'], //@note must care permission
                ],
            ],
        ]
    ]

];

注意

若需要上传第三方厂商的OSS,需要额外安装驱动,我们已经提供统一的API接口,在底层屏蔽了实现的细节。

七牛云适配器

composer require "qiniu/php-sdk:7.7.0"

Minio适配器

composer require "league/flysystem-aws-s3-v3:^3.0"

# 使用

在Controller上使用即可。

<?php

namespace app\controller;

use herosphp\annotation\Controller;
use herosphp\annotation\Get;
use herosphp\annotation\Post;
use herosphp\core\BaseController;
use herosphp\core\HttpRequest;
use herosphp\core\HttpResponse;
use herosphp\GF;
use herosphp\plugin\storage\core\Uploader;
use herosphp\plugin\storage\Storage;

#[Controller(name: UploadAction::class)]
class UploadController extends BaseController
{
    protected static Uploader $_uploader;

    public function __init()
    {
        parent::__init();

        Storage::handler('local'); // 使用本地文件上传
        static::$_uploader = Storage::getUploader();
    }


    #[Post(uri: '/upload/do')]
    public function doUpload(HttpRequest $request)
    {
        $info = static::$_uploader->upload($request->file('src'));
        if ($info === false) {
            return GF::exportVar(static::$_uploader->getError()->getMessage());
        }
        return GF::exportVar($info);
    }
}

上次更新: 10/27/2022, 11:18:25 AM