文章
位置: 首页 >文章
thinkphp5 腾讯云cos图片文件上传
- PHP
- 2021-11-11
- 1565
- 0
PHP COS图片文件上传
TP5框架使用composer命令安装vendor目录
composer require tencentcloud/tencentcloud-sdk-php
composer require qcloud/cos-sdk-v5
在application/common目录下新建一个cos目录,新建一个类文件CosSdk.php
<?php
namespace app\common\cos;
class CosSdk {
//腾讯云cos配置信息
public $QCLOUD = array(
'appId' => '', //账户中心的 APPid
'SecretId' => '', //开发者拥有的项目身份识别 ID
'SecretKey' => '', //开发者拥有的项目身份密钥
'region' => '', //你的Bucket所有地域
'bucket' => '' //COS中用于存储桶名称
);
public function __construct()
{
//引入sdk
\think\Loader::import('CosSdk.vendor.autoload');
$this->appId =$this->QCLOUD['appId'];//账户中心的 APPid
$this->SecretId =$this->QCLOUD['SecretId'];//开发者拥有的项目身份识别 ID
$this->SecretKey =$this->QCLOUD['SecretKey']; //开发者拥有的项目身份密钥
$this->region =$this->QCLOUD['region'];//你的Bucket所有地域 例如上海 (sh)写简称就可以
$this->bucket =$this->QCLOUD['bucket'];//COS中用于存储桶名称
$this->cosClient = new \Qcloud\Cos\Client(
array(
'region' => $this->region,
'credentials' => array(
'appId' => $this->appId,
'secretId' => $this->SecretId,
'secretKey' => $this->SecretKey,
)
)
);
}
/*上传文件*/
public function upload($img_url,$path_url,$type='png'){
//获取线上图片内容
if(stristr($img_url, "http")||stristr($img_url, "https")||stristr($img_url, "xiaodongai")){
//http,https的文件内容
$imgname = file_get_contents($img_url);
}else{
//二进制流内容
$imgname = $img_url;
}
//新生成的图片地址
$saveName = $path_url."/".date('YmdHis').uniqid().'.'.$type;
try {
$data = array( 'Bucket' => $this->bucket , 'Key' => $saveName, 'Body' => $imgname );
//判断文件大小 大于5M就分块上传
$result = $this->cosClient->Upload( $data['Bucket'] , $data['Key'] , $data['Body'] );
if(!empty($result["Location"])){
return json_encode(array('code' => 1, 'msg' => '上传成功', 'data' => array('url' => 'https://' . $result["Location"], 'path' => $result["Key"], 'domain' => 'backend-1255361217.cos.ap-beijing.myqcloud.com')),320);
}else {
return json_encode(array('code' => 0, 'msg' => '上传失败', 'data' => array(),320));
}
} catch (\Exception $e) {
return json_encode(array('code' => 0, 'msg' => $e->getMessage(), 'data' => $e->getMessage(),320));
}
}
/**
* [tencent_upload cos上传]
* @datetime 2021-3-19
* @return [type] [description]
*/
public function tencent_upload($file,$savePath,$url){
//获取文件名
$file_name_str = $file->getInfo()['name'];
//将字符串已点分割成数组
$file_name_arr = explode(".",$file_name_str);
//获取最后一条数组信息(文件名的后缀)
$file_name = end($file_name_arr);
$saveName = $savePath."/".date('YmdHis').uniqid().'.'.$file_name;
try {
$result= $this->cosClient->Upload($this->bucket,$saveName,$file);
if(!empty($result)){
return json_encode(['code'=>1,'msg'=>'上传成功','url'=>$url."/".$saveName]);
}else {
return json_encode(['code'=>0,'msg'=>'上传失败','url'=>'']);
}
} catch (\Exception $e) {
return json_encode(['code'=>0,'msg'=>$e->getMessage(),'url'=>'']);
}
}
}
//使用
/**
* 上传文件
*/
public function upload()
{
$file = $this->request->file('file');
$url=$this->QCLOUD['cos_pre_url'];
//调用上传sdk
$CosSdk = new CosSdk();
$splInfo=$CosSdk->tencent_upload($file,'product',$url);
}
Tags: javascript thinkPHP cos
转载:欢迎来到本站,转载请注明文章出处https://www.ormcc.com ,欢迎加入技术讨论群599606903
下一篇:CSS跳动的文字
文章评论
- 0
- 0
- 0
- 分享
- 打赏
ormcc
一个爱捣鼓的程序员
- 160
会员 - 87
今日访问 - 607
文章
IP访问121772次,运行1480天
评论排行
文章归档
- 还没有相关文章