php一个可自定义上传驱动的文件上传类

没有依赖任何框架的函数,因此所有的框架基本都兼容

目前测试通过的框架thinkphp全家桶,laravel框架。

github地址: https://github.com/tp5er/file

安装

composer require tp5er/file dev-master

文件上传

$file = $_FILES['image'];
$path =  './uploads';
$info = (new \tp5er\FileApp())->fileupload($file, $path);


//输出  jpg
echo $info->getExtension();

//输出  ea307c4cdc7f7e91e8578caad1db1240.jpg
echo $info->getFilename();

安全校验

$info = (new \tp5er\FileApp())->fileupload($file, $path,['type'=>"image/png",'size'=>15678,'ext'=>'jpg,png,gif']);

COS上传

//首先安装 composer require qcloud/cos-sdk-v5

上传方法
$info = (new \tp5er\FileApp(new \tp5er\drive\Cos([
    'SecretId'=>'',
    'SecretKey'=>"",
    'bucket'=>"",
    'region'=>''
])))->fileupload($file, $path);

自定义上传驱动(COS)

官方文档:https://cloud.tencent.com/doc...

包地址:https://packagist.org/package...

安装

composer require qcloud/cos-sdk-v5

接口实现类

 $region,
                'schema' => 'https',
                'credentials' => [
                    'secretId' => $secretId,
                    'secretKey' => $secretKey
                ]
            ]);
            $key = $fullname;
            $body = file_get_contents($file['tmp_name']);
            $result = $cosClient->putObject(array('Bucket' => $bucket, 'Key' => $key, 'Body' => $body));
            if (!isset($result['Location']) && !isset($result['Key'])){
                trigger_error("upload write error");
                return false;
            }
            return true;
        }catch (Exception $exception){
            throw  new  \Exception($exception);
        }
    }
}

传入接口实现类实现上传功能

$info = (new \tp5er\FileApp(new \Cos()))->fileupload($file, $path);

官方sdk汇总

阿里云oss

https://help.aliyun.com/docum...

七牛云

https://developer.qiniu.com/k...

京东云

https://docs.jdcloud.com/cn/o...

你可能感兴趣的:(文件上传,php)