仓库初始化
This commit is contained in:
48
application/common/controller/Upload.php
Normal file
48
application/common/controller/Upload.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user