Files

48 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2025-08-07 20:21:47 +08:00
<?php
namespace app\common\controller;
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
class Upload
{
// 显式声明属性
private $config;
private $ossClient; // 修复点:添加该属性声明
private $bucket;
public function __construct()
{
$this->config = get_system_config();
$endpoint = $this->config['oss_region_url'];
$this->bucket= $this->config['oss_bucket_name'];
//获取配置
$this->ossClient = new OssClient(
$this->config['oss_access_key_id'],
$this->config['oss_access_key_secret'],
$endpoint
);
}
/*
* 上传文件
* 参数:
* $bucket: 存储空间名称
* $object: 文件名
* $filePath: 文件路径
* 返回:
* true: 上传成功
* false: 上传失败
*/
public function uploadFile($object, $filePath) {
try {
$result = $this->ossClient->uploadFile($this->bucket, $object, $filePath);
return true; // 上传成功返回true
} catch (OssException $e) {
return false; // 上传失败返回false并记录错误信息例如echo $e->getMessage();
}
}
}