Files
yusheng-php/application/adminapi/controller/UploadFile.php
2025-08-07 20:21:47 +08:00

49 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\adminapi\controller;
use app\admin\model\AdminLog;
use app\common\controller\Upload;
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();
$objectName = $file->getInfo('name');
// 调用父类方法上传到 OSS
$result = $this->uploadFile($objectName, $filePath);
if (!$result) {
return V(0, '上传失败请检查OSS配置或网络', null);
}
// 返回访问地址
$url = $this->config['oss_cdn_url'] . $objectName;
return V(1, '上传成功', ['url' => $url]);
} catch (\Exception $e) {
return V(0, '上传异常: ' . $e->getMessage(), null);
}
}
}