正在显示
1 个修改的文件
包含
18 行增加
和
33 行删除
| @@ -40,23 +40,16 @@ class TencentCosService extends BaseService | @@ -40,23 +40,16 @@ class TencentCosService extends BaseService | ||
| 40 | * @method :post | 40 | * @method :post |
| 41 | * @time :2023/7/18 16:56 | 41 | * @time :2023/7/18 16:56 |
| 42 | */ | 42 | */ |
| 43 | - public function upload_image($path,$fileName,$image_type){ | 43 | + public function upload_image($path,$fileName){ |
| 44 | // 构建请求URL | 44 | // 构建请求URL |
| 45 | $expiredTime = time() + 3600; | 45 | $expiredTime = time() + 3600; |
| 46 | $pathname = '/'.$this->config['bucket'].'/'.$fileName; | 46 | $pathname = '/'.$this->config['bucket'].'/'.$fileName; |
| 47 | - $signature = $this->generateSignature($this->config['secretKey'], 'PUT', $pathname, $expiredTime); | 47 | + $signature = $this->generateSignature($this->config['secretKey'], $pathname, $expiredTime); |
| 48 | $url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname.'?sign='.$signature; | 48 | $url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname.'?sign='.$signature; |
| 49 | - // 构造请求方法、URL和头部 | ||
| 50 | - $url = 'https://' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'.$path; | ||
| 51 | - $this->method = 'PUT'; | ||
| 52 | - $headers = array( | ||
| 53 | - 'Authorization: ' . $this->cosAuthorization($this->config['appId'], $this->config['secretId'], $this->config['secretKey'], $this->method, '/' . $this->config['bucket'] . $path), | ||
| 54 | - 'Content-Type: image/'.$image_type // 根据上传的文件类型设置相应的Content-Type | ||
| 55 | - ); | ||
| 56 | // 打开文件流 | 49 | // 打开文件流 |
| 57 | $url_path = config('filesystems.disks.upload')['root'].$path; | 50 | $url_path = config('filesystems.disks.upload')['root'].$path; |
| 58 | - $file = fopen($url_path, 'r'); | ||
| 59 | - return $this->http_put($url,$headers,$file,$url_path); | 51 | + $fileContent = file_get_contents($url_path); |
| 52 | + return $this->http_put($url,$fileContent); | ||
| 60 | } | 53 | } |
| 61 | 54 | ||
| 62 | /** | 55 | /** |
| @@ -66,27 +59,19 @@ class TencentCosService extends BaseService | @@ -66,27 +59,19 @@ class TencentCosService extends BaseService | ||
| 66 | * @method :post | 59 | * @method :post |
| 67 | * @time :2023/7/18 17:06 | 60 | * @time :2023/7/18 17:06 |
| 68 | */ | 61 | */ |
| 69 | - public function http_put($url,$headers,$file,$url_path){ | ||
| 70 | - // 创建CURL句柄 | ||
| 71 | - $curl = curl_init(); | ||
| 72 | - // 设置请求方法、URL、头部和数据流 | ||
| 73 | - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); | ||
| 74 | - curl_setopt($curl, CURLOPT_URL, $url); | ||
| 75 | - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | ||
| 76 | - curl_setopt($curl, CURLOPT_INFILE, $file); | ||
| 77 | - curl_setopt($curl, CURLOPT_INFILESIZE, filesize($url_path)); | ||
| 78 | - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
| 79 | - // 发起请求 | ||
| 80 | - curl_exec($curl); | ||
| 81 | - // 关闭文件流和CURL句柄 | ||
| 82 | - fclose($file); | ||
| 83 | - curl_close($curl); | ||
| 84 | - // 检查响应状态码 | ||
| 85 | - $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||
| 86 | - if ($httpCode == 200) { | ||
| 87 | - return 1; | 62 | + public function http_put($url,$fileContent){ |
| 63 | + $ch = curl_init(); | ||
| 64 | + curl_setopt($ch, CURLOPT_URL, $url); | ||
| 65 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | ||
| 66 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
| 67 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContent); | ||
| 68 | + $response = curl_exec($ch); | ||
| 69 | + curl_close($ch); | ||
| 70 | + // 检查上传结果 | ||
| 71 | + if ($response === false) { | ||
| 72 | + echo '图片上传失败'; | ||
| 88 | } else { | 73 | } else { |
| 89 | - return 2; | 74 | + echo '图片上传成功'; |
| 90 | } | 75 | } |
| 91 | } | 76 | } |
| 92 | 77 | ||
| @@ -97,9 +82,9 @@ class TencentCosService extends BaseService | @@ -97,9 +82,9 @@ class TencentCosService extends BaseService | ||
| 97 | * @method :post | 82 | * @method :post |
| 98 | * @time :2023/7/18 17:10 | 83 | * @time :2023/7/18 17:10 |
| 99 | */ | 84 | */ |
| 100 | - public function generateSignature($secretKey, $method, $pathname, $expiredTime) { | 85 | + public function generateSignature($secretKey, $pathname, $path ,$expiredTime) { |
| 101 | $signTime = time(); | 86 | $signTime = time(); |
| 102 | - $plainText = "a=".$secretId."&k=".$secretKey."&e=".$expiredTime."&t=".$signTime."&r=".$pathname."&f="; | 87 | + $plainText = "a=".$this->config['secretId']."&k=".$secretKey."&e=".$expiredTime."&t=".$signTime."&r=".$pathname."&f=".$path; |
| 103 | $bin = hash_hmac("SHA1", $plainText, $secretKey, true); | 88 | $bin = hash_hmac("SHA1", $plainText, $secretKey, true); |
| 104 | $bin = $bin . $plainText; | 89 | $bin = $bin . $plainText; |
| 105 | $sign = base64_encode($bin); | 90 | $sign = base64_encode($bin); |
-
请 注册 或 登录 后发表评论