基于webman整合的league/flysystem V2版本的本地上传/cos/oss/qiniu等(新增便捷/批量上传)

TycoonSong

已上架插件市场 可取插件市场 插件市场

安装

composer require shopwwi/webman-filesystem

使用方法

  • 阿里云 OSS 适配器
composer require shopwwi/flysystem-oss
  • S3 适配器
composer require "league/flysystem-aws-s3-v3:^2.0"
  • 七牛云适配器
composer require "overtrue/flysystem-qiniu:^2.0"
  • 内存适配器
composer require "league/flysystem-memory:^2.0"
  • 腾讯云 COS 适配器
composer require "overtrue/flysystem-cos:^4.0"

使用

通过FilesystemFactory::get('local') 来调用不同的适配器

    use Shopwwi\WebmanFilesystem\FilesystemFactory;
    public function upload(Request $request)
    {
        $file = $request->file('file');

        $filesystem =  FilesystemFactory::get('local');
        $stream = fopen($file->getRealPath(), 'r+');
        $filesystem->writeStream(
            'uploads/'.$file->getUploadName(),
            $stream
        );
        fclose($stream);

        // Write Files
        $filesystem->write('path/to/file.txt', 'contents');

        // Add local file
        $stream = fopen('local/path/to/file.txt', 'r+');
        $result = $filesystem->writeStream('path/to/file.txt', $stream);
        if (is_resource($stream)) {
            fclose($stream);
        }

        // Update Files
        $filesystem->update('path/to/file.txt', 'new contents');

        // Check if a file exists
        $exists = $filesystem->has('path/to/file.txt');

        // Read Files
        $contents = $filesystem->read('path/to/file.txt');

        // Delete Files
        $filesystem->delete('path/to/file.txt');

        // Rename Files
        $filesystem->rename('filename.txt', 'newname.txt');

        // Copy Files
        $filesystem->copy('filename.txt', 'duplicate.txt');

        // list the contents
        $filesystem->listContents('path', false);
    }

便捷式上传

    use Shopwwi\WebmanFilesystem\Facade\Storage;
    public function upload(\support\Request $request){
        //单文件上传
        $file = $request->file('file');
        $result = Storage::upload($file);
        //单文件判断
        try {
            $result = Storage::adapter('local')->path('upload/user')->size(1024*1024*5)->extYes(['image/jpeg','image/gif'])->extNo(['image/png'])->upload($file);
         }catch (\Exception $e){
            $e->getMessage();
         }

         //多文件上传
         $files = $request->file();
         $result = Storage::uploads($files);
         try {
         //uploads 第二个参数为限制文件数量 比如设置为10 则只允许上传10个文件 第三个参数为允许上传总大小 则本列表中文件总大小不得超过设定
            $result = Storage::adapter('local')->path('upload/user')->size(1024*1024*5)->extYes(['image/jpeg','image/gif'])->extNo(['image/png'])->uploads($files,10,1024*1024*100);
         }catch (\Exception $e){
            $e->getMessage();
         }

         //获取文件外网
         $filesName = 'storage/a4bab140776e0c1d57cc316266e1ca05.png';
         $fileUrl = Storage::url($filesName);
         //指定选定器外网
         $fileUrl = Storage::adapter('oss')->url($filesName);
    }

静态方法(可单独设定)

方法 描述 默认
adapter 选定器 config中配置的default
size 单文件大小 config中配置的max_size
extYes 允许上传文件类型 config中配置的ext_yes
extNo 不允许上传文件类型 config中配置的ext_no
path 文件存放路径(非完整路径) storage

响应字段

字段 描述 示例值
origin_name 源文件名称 webman.png
file_name 文件路径及名称 storage/a4bab140776e0c1d57cc316266e1ca05.png
storage_key 文件随机key a4bab140776e0c1d57cc316266e1ca05
file_url 文件访问外网 //127.0.0.1:8787/storage/cab473e23b638c2ad2ad58115e28251c.png
size 文件大小 22175
mime_type 文件类型 image/jpeg
extension 文件尾缀 jpg
width 图片宽度(图片类型才返回) 206
height 图片高度(图片类型才返回) 206
1576 4 0
4个评论

walkor

感谢分享

  • 暂无评论
TycoonSong

新增便捷快速批量上传

便捷式上传

    use Shopwwi\WebmanFilesystem\Facade\Storage;
    public function upload(\support\Request $request){
        //单文件上传
        $file = $request->file('file');
        $result = Storage::upload($file);
        //单文件判断
        try {
            $result = Storage::adapter('local')->path('upload/user')->size(1024*1024*5)->extYes(['image/jpeg','image/gif'])->extNo(['image/png'])->upload($file);
         }catch (\Exception $e){
            $e->getMessage();
         }

         //多文件上传
         $files = $request->file();
         $result = Storage::uploads($files);
         try {
         //uploads 第二个参数为限制文件数量 比如设置为10 则只允许上传10个文件 第三个参数为允许上传总大小 则本列表中文件总大小不得超过设定
            $result = Storage::adapter('local')->path('upload/user')->size(1024*1024*5)->extYes(['image/jpeg','image/gif'])->extNo(['image/png'])->uploads($files,10,1024*1024*100);
         }catch (\Exception $e){
            $e->getMessage();
         }

         //获取文件外网
         $filesName = 'storage/a4bab140776e0c1d57cc316266e1ca05.png';
         $fileUrl = Storage::url($filesName);
         //指定选定器外网
         $fileUrl = Storage::adapter('oss')->url($filesName);
    }

静态方法(可单独设定)

方法 描述 默认
adapter 选定器 config中配置的default
size 单文件大小 config中配置的max_size
extYes 允许上传文件类型 config中配置的ext_yes
extNo 不允许上传文件类型 config中配置的ext_no
path 文件存放路径(非完整路径) storage

响应字段

字段 描述 示例值
origin_name 源文件名称 webman.png
file_name 文件路径及名称 storage/a4bab140776e0c1d57cc316266e1ca05.png
storage_key 文件随机key a4bab140776e0c1d57cc316266e1ca05
file_url 文件访问外网 //127.0.0.1:8787/storage/cab473e23b638c2ad2ad58115e28251c.png
size 文件大小 22175
mime_type 文件类型 image/jpeg
extension 文件尾缀 jpg
width 图片宽度(图片类型才返回) 206
height 图片高度(图片类型才返回) 206
  • 暂无评论
嘿嘿
"php": ">=8.1",

2022-04-05 12:43:59 pid:1 Worker process terminated with ERROR: E_ERROR "During inheritance of ArrayAccess: Uncaught ErrorException: Return type of Qiniu\Http\Header::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Http\Header.php:114

Stack trace:

0 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Http\Header.php(8): {closure}(8192, 'Return type of ...', 'D:\laragon\www\...', 114)

1 D:\laragon\www\dev\vendor\composer\ClassLoader.php(571): include('D:\laragon\www\...')

2 D:\laragon\www\dev\vendor\composer\ClassLoader.php(428): Composer\Autoload\includeFile('D:\laragon\www\...')

3 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Http\Client.php(128): Composer\Autoload\ClassLoader->loadClass('Qiniu\Http\Head...')

4 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Http\Client.php(13): Qiniu\Http\Client::sendRequest(Object(Qiniu\Http\Request))

5 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Region.php(147): Qiniu\Http\Client::get('api.qiniu.com/v...')

6 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Zone.php(45): Qiniu\Region::queryRegion('qxMhaL4C7EoaG...', 'woisks')

7 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Config.php(135): Qiniu\Zone::queryZone('qxMhaL4C7EoaG...', 'woisks')

8 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Config.php(41): Qiniu\Config->getRegion('qxMhaL4C7EoaG...', 'woisks')

9 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Storage\FormUploader.php(61): Qiniu\Config->getUpHost('qxMhaL4C7EoaG...', 'woisks')

10 D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Storage\UploadManager.php(116): Qiniu\Storage\FormUploader::put('qxMhaL4C7EoaG...', 'avatar', '\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x00\x00...', Object(Qiniu\Config), NULL, 'application/oct...', 'wor9AC9.tmp')

11 D:\laragon\www\dev\app\passport\controller\State.php(42): Qiniu\Storage\UploadManager->putFile('qxMhaL4C7EoaG...', 'avatar', Object(Webman\Http\UploadFile))

12 D:\laragon\www\dev\framework-webman\App.php(331): app\passport\controller\State->online(Object(Webman\Http\Request))

13 D:\laragon\www\dev\framework-webman\App.php(146): Webman\App::findRoute(Object(Workerman\Connection\TcpConnection), '/passport/onlin...', 'POST/passport/o...', Object(Webman\Http\Request))

14 D:\laragon\www\dev\vendor\workerman\workerman\Connection\TcpConnection.php(638): Webman\App->onMessage(Object(Workerman\Connection\TcpConnection), Object(Webman\Http\Request))

15 D:\laragon\www\dev\vendor\workerman\workerman\Events\Select.php(295): Workerman\Connection\TcpConnection->baseRead(Resource id #196)

16 D:\laragon\www\dev\vendor\workerman\workerman\Worker.php(2431): Workerman\Events\Select->loop()

17 D:\laragon\www\dev\vendor\workerman\workerman\Worker.php(1430): Workerman\Worker->run()

18 D:\laragon\www\dev\vendor\workerman\workerman\Worker.php(1373): Workerman\Worker::forkWorkersForWindows()

19 D:\laragon\www\dev\vendor\workerman\workerman\Worker.php(549): Workerman\Worker::forkWorkers()

20 D:\laragon\www\dev\start.php(94): Workerman\Worker::runAll()

21 {main} in D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Http\Header.php on line 8"

2022-04-05 12:44:00 pid:1 Worker process terminated with ERROR: E_ERROR "During inheritance of ArrayAccess: Uncaught ErrorException: Return type of Qiniu\Http\Header::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in D:\laragon\www\dev\vendor\qiniu\php-sdk\src\Qiniu\Http\Header.php:114

  • 嘿嘿 2022-04-05

    七牛云上传失败 的信息

  • TycoonSong 2022-04-08

    如果需支持8.0的选定器需安装composer require "overtrue/flysystem-qiniu:^3.0"

auithira

便捷式上传报错
指定适配器时
$path = Storage::adapter('qiniu')->path($storePath)->upload($image);
Undefined array key \"qiniu\
截图
默认适配器时
截图
截图

  • 暂无评论

TycoonSong

684
积分
0
获赞数
0
粉丝数
2021-10-29 加入
🔝