Files
SoonDesign/api/read_file.php
T
24kycj 2e6578247e 网页端:平台桥与部署修复(宝塔/Nginx)
- lib/platform:网页 Electron 统一 bridge/web/electron,子目录 API 基路径、返回首页跳站点根、路径末尾斜杠兼容
- index/design*.web.html:网页入口,web.js 加缓存参数避免旧脚本缓存
- api:PHP 读写 soon;文档说明目录权限与 Nginx
- lib/design*、lib/index:与 platformBridge 对接及网页侧逻辑
- 公共资源 JsBarcode/jr-qrcode;文档与 package/README/.gitignore 等同步更新

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-09 18:57:53 +08:00

76 lines
2.0 KiB
PHP

<?php
/**
* SoonDesign 网页端文件读取接口
* 从服务器读取 .soon 文件
*/
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
// 处理 OPTIONS 预检请求
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// 只接受 GET 请求
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
exit;
}
// 获取文件名
$fileName = isset($_GET['fileName']) ? $_GET['fileName'] : '';
// 验证文件名
if (empty($fileName)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => '文件名不能为空']);
exit;
}
// 清理文件名,防止路径遍历攻击
$fileName = basename($fileName);
if (strpos($fileName, '..') !== false) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => '无效的文件名']);
exit;
}
// 获取文件路径
$saveDir = dirname(__DIR__) . '/design/files/';
$filePath = $saveDir . $fileName;
// 检查文件是否存在
if (!file_exists($filePath)) {
http_response_code(404);
echo json_encode(['success' => false, 'error' => '文件不存在']);
exit;
}
// 读取文件内容
try {
$content = file_get_contents($filePath);
if ($content === false) {
throw new Exception('文件读取失败');
}
// 尝试解析 JSON 验证格式
$json = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('文件格式错误: ' . json_last_error_msg());
}
echo $content;
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'error' => '读取失败: ' . $e->getMessage()
]);
}
?>