1、安装插件 2020年8月6日14:36:13
"require": {
"php": "^7.2",
"barryvdh/laravel-debugbar": "^3.2",
"fideloper/proxy": "^4.0",
"freyo/flysystem-qcloud-cos-v5": "^2.0",
"fruitcake/laravel-cors": "^2.0",
"jacobcyl/ali-oss-storage": "^2.1",
"johnlui/aliyun-oss": "~2.0",
"laravel/framework": "^6.0",
"laravel/tinker": "^1.0",
"mews/captcha": "^3.0",
"overtrue/wechat": "~4.0",
"php-amqplib/php-amqplib": "^2.11",
"phpoffice/phpspreadsheet": "^1.9",
"spatie/laravel-permission": "^3.0",
"vladimir-yuldashev/laravel-queue-rabbitmq": "^10.2"
},
2、封装类
<?php
namespace App\Services;
use App\Helpers\Tool;
use Qcloud\Cos\Client;
/**
* 2020-7-17 19:27:38 腾讯云cos上传文件
* Class QcloudServices
* @package App\Services
*/
class QcloudServices
{
protected $user;
protected $cosClient;
public function __construct()
{
$secretId = env('COSV5_SECRET_ID');
$secretKey = env('COSV5_SECRET_KEY');
$region = env('COSV5_REGION', 'ap-guangzhou');
$this->cosClient = new Client(
array(
'region' => $region,
'schema' => env('COSV5_SCHEME', 'https'),
'credentials' => array(
'secretId' => $secretId,
'secretKey' => $secretKey)
));
}
/**
* 上传图片的到腾讯云 COS
* @param $file
* @return bool|string
*/
public function upload_cos($file)
{
if (empty($file)) return false;
try {
$originalName = $file->getClientOriginalName(); // 文件原名
$ext = $file->getClientOriginalExtension(); // 扩展名
$realPath = $file->getRealPath(); // 临时文件的绝对路径
$data['file_name'] = $originalName;
$data['file_type'] = $ext;
$bucket = env('COSV5_BUCKET');
$filename = get_new_filename($ext);
$ossKey = Tool::getOssDir($ext);
$key = $ossKey . $filename;
$result = $this->cosClient->putObject(array(
'Bucket' =&

这篇博客讲述了如何在 Laravel 6.18 框架中使用插件进行文件上传,特别是针对阿里云OSS和腾讯云COS的文件上传操作。作者遇到问题,上传的.amr和图片文件后缀名被改变,并询问了解决方案。
最低0.47元/天 解锁文章
3356

被折叠的 条评论
为什么被折叠?



