Files
yusheng-php/application/adminapi/controller/UploadFile.php
2025-10-20 18:11:39 +08:00

75 lines
2.5 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\adminapi\controller;
use app\admin\model\AdminLog;
use app\common\controller\Upload;
use Overtrue\Pinyin\Pinyin;
use think\Request;
/**
* 后台首页
* @internal
*/
class UploadFile extends Upload
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
parent::__construct();
$this->config = get_system_config();
}
/*
* 上传文件
* @return array
*/
public function file_upload(){
// 获取上传的文件对象
$file = $this->request->file('files');
if (!$file) {
return V(0, '未上传文件', null);
}
try {
// 获取临时路径和原始文件名
$filePath = $file->getRealPath();
$originalName = $file->getInfo('name');
// 提取文件扩展名
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
// 处理文件名(中文转拼音) 移除扩展名(如果有的话)
$fileName = pathinfo($originalName, PATHINFO_FILENAME);
if (preg_match('/[\x{4e00}-\x{9fa5}]+/u', $fileName)) {
$pinyin = new Pinyin();
$fileName = $pinyin->permalink($fileName, ''); // 转换为无分隔符的拼音
}
// 去除文件名中的空格
$fileName = str_replace(' ', '', $fileName);
// 添加唯一标识符(时间戳+随机数)
$uniqueId = time() . mt_rand(1000, 9999);
$objectName = $fileName . '_' . $uniqueId . '.' . $extension;
// 调用父类方法上传到 OSS
// $result = $this->uploadFile($objectName, $filePath);
// if (!$result) {
// return V(0, '上传失败请检查OSS配置或网络', null);
// }
// // 返回访问地址
// $url = $this->config['oss_cdn_url'] . $objectName;
// 调用父类方法上传到 COS
$result = $this->uploadFileCos($objectName, $filePath);
if (!$result) {
return V(0, '上传失败请检查COS配置或网络', null);
}
$url = $result;// 返回访问地址 具体访问地址根据你的实际配置来定这个需要修改 等域名通过备案后修改
return V(1, '上传成功', ['url' => $url]);
} catch (\Exception $e) {
return V(0, '上传异常: ' . $e->getMessage(), null);
}
}
}