文件上传
This commit is contained in:
parent
2dc378734f
commit
624a2628d0
98
webman/app/controller/UploadController.php
Normal file
98
webman/app/controller/UploadController.php
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\controller;
|
||||||
|
|
||||||
|
//use support\Request;
|
||||||
|
//use support\Log;
|
||||||
|
//use Webman\Http\Response;
|
||||||
|
|
||||||
|
use app\util\Util;
|
||||||
|
use support\Log;
|
||||||
|
use support\Request;
|
||||||
|
|
||||||
|
class UploadController
|
||||||
|
{
|
||||||
|
// 允许的文件类型
|
||||||
|
protected $allowedMimeTypes = [
|
||||||
|
'image/jpeg',
|
||||||
|
'image/jpg',
|
||||||
|
'image/png',
|
||||||
|
'image/gif',
|
||||||
|
'application/pdf',
|
||||||
|
'text/plain'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 最大文件大小(5MB)
|
||||||
|
protected $maxFileSize = 5 * 1024 * 1024;
|
||||||
|
|
||||||
|
public function upload(Request $request): \support\Response
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// 获取上传的文件(支持多文件)
|
||||||
|
$files = $request->file('files');
|
||||||
|
|
||||||
|
// 如果没有文件上传
|
||||||
|
if (!$files) {
|
||||||
|
throw new \Exception('未上传任何文件');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单文件转换为数组统一处理
|
||||||
|
if (!is_array($files)) {
|
||||||
|
$files = [$files];
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
foreach ($files as $file) {
|
||||||
|
// 检查文件是否上传成功
|
||||||
|
if (!$file->isValid()) {
|
||||||
|
throw new \Exception($file->getUploadErrorMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验文件类型
|
||||||
|
if (!in_array($file->getUploadMineType(), $this->allowedMimeTypes)) {
|
||||||
|
throw new \Exception('不支持的文件类型: ' . $file->getUploadMineType());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验文件大小
|
||||||
|
$fileSize = $file->getSize();
|
||||||
|
if ($fileSize > $this->maxFileSize) {
|
||||||
|
throw new \Exception('文件大小超过限制');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成唯一文件名(防止覆盖)
|
||||||
|
$filename = uniqid() . '.' . $file->getUploadExtension();
|
||||||
|
|
||||||
|
// 保存路径(例如:runtime/uploads/202310)
|
||||||
|
$savePath = runtime_path('uploads/' . date('Ym/d'));
|
||||||
|
if (!is_dir($savePath)) {
|
||||||
|
mkdir($savePath, 0755, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移动文件到目标路径
|
||||||
|
$file->move($savePath . '/' . $filename);
|
||||||
|
|
||||||
|
// 记录上传信息
|
||||||
|
Log::info('文件上传成功', [
|
||||||
|
'original_name' => $file->getUploadName(),
|
||||||
|
'saved_path' => $savePath . '/' . $filename,
|
||||||
|
'size' => $fileSize
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result[] = [
|
||||||
|
'original_name' => $file->getUploadName(),
|
||||||
|
'saved_name' => $filename,
|
||||||
|
'path' => 'uploads/' . date('Ym') . '/' . $filename,
|
||||||
|
'size' => $fileSize,
|
||||||
|
'mime_type' => $file->getUploadMineType()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Util::success($result,'上传成功');
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Log::error('文件上传失败: ' . $e->getMessage());
|
||||||
|
|
||||||
|
return Util::fail([],$e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
use Webman\Route;
|
use Webman\Route;
|
||||||
|
|
||||||
|
// 文件上传接口
|
||||||
|
Route::post('/upload', [app\controller\UploadController::class, 'upload']);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user