Files
midi-php/application/common/controller/Upload.php
2025-08-13 10:43:56 +08:00

48 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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();
}
}
}