dlh_test_server/app/common/utils/UploadAliOSS.php
2024-08-14 18:34:36 +08:00

59 lines
1.7 KiB
PHP

<?php
namespace app\common\utils;
use app\common\server\storage\Driver as StorageDriver;
use think\facade\Config;
class UploadAliOSS
{
// 删除图片
public static function uploadImage($path = '')
{
if (!$path) {
return ['code' => -1, 'msg' => '缺少上传路径'];
} else {
$StorageDriver = new StorageDriver(self::config());
$StorageDriver->setUploadFile('file');
if (!$StorageDriver->upload($path)) {
return ['code' => -1, 'msg' => '上传错误'];
}
$fileName = $StorageDriver->getFileName();
if ($fileName) {
return ['code' => 1, 'msg' => '', 'data' => array('file' => Config::get('aliyunoss.domain') . $path . '/'. $fileName)];
}
return ['code' => -1, 'msg' => '上传失败'];
}
}
// 删除文件
public static function delFile($path, $file)
{
if (!$path) {
return ['code' => -1, 'msg' => '缺少上传路径'];
} else {
$file = $path . '/' . basename($file);
$StorageDriver = new StorageDriver(self::config());
$StorageDriver->delete($file);
}
return ['code' => 1, 'msg' => '删除成功'];
}
private static function config()
{
return [
"default" => "aliyun",
"engine" => [
"aliyun" => [
"bucket" => Config::get('aliyunoss.bucket'),
"access_key_id" => Config::get('aliyunoss.access_key_id'),
"access_key_secret" => Config::get('aliyunoss.access_key_secret'),
"domain" => Config::get('aliyunoss.domain'),
]
]
];
}
}