You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无语法错误的PHP文件结合自定义router.php出现"Unexpected end of file"错误

Fixing the "Unexpected end of file" Error in PHP Built-in Server's router.php

Hey there, let's get this sorted out quickly! The "Unexpected end of file" error you're seeing isn't from your original PHP files—it's coming directly from your incomplete router.php script. Here's what's going on and how to fix it:

The Root Cause

Looking at your code snippet, the last line cuts off mid-statement with if...—PHP can't parse an unfinished conditional block, so it throws this error when reaching the end of the file without finding the necessary closing syntax (like curly braces or the rest of the condition logic).

How to Fix It

You need to complete the conditional logic, especially the part that handles non-existent files (which is where your custom 404 page comes in). Here's a full, corrected version of the router script that implements the missing pieces:

<?php
$root = $_SERVER['DOCUMENT_ROOT'];
chdir($root);
$path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'])['path'],'/');
set_include_path(get_include_path().':'.__DIR__);

if (file_exists($root.$path)) {
    // Handle directories without trailing slashes
    if (is_dir($root.$path) && substr($path, -1) !== '/') {
        $path = rtrim($path, '/').'/index.php';
    }
    // Serve the existing file/directory index
    include $root.$path;
} else {
    // Trigger custom 404 response
    http_response_code(404);
    // Replace this with your actual 404 file path or content
    include __DIR__.'/404.php';
    // Or directly output content if you prefer:
    // echo "<!DOCTYPE html><html><head><title>404 Not Found</title></head><body><h1>404 - Page Not Found</h1></body></html>";
}

Key Notes

  • Always double-check for unclosed curly braces {} or incomplete statements when modifying router scripts—even a missing closing brace can break the entire server's request handling.
  • The error appears on your "原本无语法错误的PHP文件" because the router script intercepts all requests before they reach your target files. If the router itself has syntax issues, PHP can't proceed to serve your actual files.

内容的提问来源于stack exchange,提问作者Elijah Bansley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:37:49