php将图片存储在阿里云oss存储上

  

创建两个方法

1.上传方法

 1 use OSS\OssClient;
 2 use think\Config;
 3 use OSS\Core\OssException;
 4 /**
 5  * 存储文件
 6  *
 7  * @param $srcFile 
 8  * @param $desFile
 9  * @throws Exception
10  */
11 public function storage_save($srcPath, $desPath)
12 {   //配置
13     $accessKeyId  = '';
14     $accessKeySecret  = '';
15     $endpoint = '';
16     $bucket  = '';
17 
18     $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
19 
20     $ossClient->uploadFile($bucket, $desPath, $srcPath);
21 }

 

2.删除方法

/**
* 删除文件
*
* @param $srcFile 文件路径
*
*/
public function storage_delete($desPath)
{

//配置
    $accessKeyId  = '';
$accessKeySecret = '';
$endpoint = '';
$bucket = '';


$ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);

$ossClient->deleteObject($bucket, $desPath);
}

方法的使用
/*  $data["tmp_name"]=>"/work/web/shop/public/upload/goods/2018/07/phptQIOdq_5b470a5b98bef8_60278656.jpg" //本地文件地址
$data["src"]=> "upload/goods/2018/07/phptQIOdq_5b470a5b98bef8_60278656.jpg" //上传后的图片文件
*/

//上传阿里云OSS存储
$this->storage_save($data['tmp_name'], $data['src']);


posted @ 2018-07-12 16:44  Mr、桔子  阅读(3335)  评论(2编辑  收藏  举报