将原有的/system/upload.php修改为下面代码
<?php
// TinyMCE 上传接口(最终自动域名版)
ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_NOTICE);
header('Content-Type: application/json; charset=utf-8');
require(__DIR__ . '/common.php');
require(__DIR__ . '/../source/include/upload.php');
// 自动检测当前站点 URL(含协议与域名)
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$baseUrl = $scheme . '://' . $host;
// 如果 options 未定义,使用默认值
if (!isset($options) || !is_array($options)) {
$options = [
'upload_dir' => 'uploads',
'site_url' => $baseUrl
];
}
if (isset($_GET['act']) && $_GET['act'] === 'upload') {
$savepath = '../' . $options['upload_dir'] . '/article/';
$upload = new upload_file();
$upload->make_dir($savepath);
$file_field = isset($_FILES['imgFile']) ? 'imgFile' : 'file';
if (!isset($_FILES[$file_field])) {
http_response_code(400);
echo json_encode(['error' => '未检测到上传文件']);
exit;
}
$upload->init($_FILES[$file_field], $savepath);
if ($upload->error_code === 0) {
$upload->save_file();
$relative_path = str_replace('../', '', $upload->attach['path']);
$image_url = rtrim($options['site_url'], '/') . '/' . ltrim($relative_path, '/');
echo json_encode(['location' => $image_url], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
exit;
} else {
http_response_code(400);
echo json_encode(['error' => $upload->error()], JSON_UNESCAPED_UNICODE);
exit;
}
}
?>