slink / router.php
asfag654's picture
Update router.php
cac2eee verified
<?php
// 设置时区
date_default_timezone_set('Asia/Shanghai');
// 处理错误输出
ini_set('display_errors', 'Off');
error_reporting(E_ALL);
ini_set('log_errors', 'On');
ini_set('error_log', '/dev/stderr');
// 设置 Content-Type 为 JSON(针对 API 请求)
if (in_array(basename($_SERVER['REQUEST_URI']), [
'delete.php',
'images.php',
'videos.php',
'upload.php',
'random-image.php' // 新增的路由
])) {
header('Content-Type: application/json');
}
// 处理静态文件
if (preg_match('/\.(css|js|png|jpg|jpeg|gif|ico|webp|mp4)$/', $_SERVER['REQUEST_URI'])) {
return false; // 让 PHP 内置服务器处理静态文件
}
// 根据请求路径加载对应的 PHP 文件
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$file = ltrim($path, '/');
// API 路由映射
$apiRoutes = [
'delete.php' => 'delete.php',
'images.php' => 'images.php',
'img.php' => 'img.php',
'upload.php' => 'upload.php',
'videos.php' => 'videos.php',
'adminer.php' => 'adminer.php'
];
// 处理 API 请求
if (isset($apiRoutes[$file])) {
require __DIR__ . '/' . $apiRoutes[$file];
return true;
}
// 如果是空路径或根路径,返回 index.html
if ($file === '' || $file === 'index.html') {
require __DIR__ . '/index.html';
return true;
}
// 如果文件存在且是 PHP 文件,则执行它
if (file_exists(__DIR__ . '/' . $file) && preg_match('/\.php$/', $file)) {
require __DIR__ . '/' . $file;
return true;
}
// 默认返回 index.html
require __DIR__ . '/index.html';
return true;