Parse error: syntax error, unexpected end of file in C:\Program Files\XAMPP\htdocs\lookup.php on line 116
This specific error messageâParse error: syntax error, unexpected end of file at line 116âis a classic indicator that PHP encountered code it couldn't parse before reaching the very end of the script. While the error points to line 116, the actual problem in this case is almost certainly located earlier in the file. The parser needs a balanced set of opening and closing tags; if you miss a ?>, the engine assumes the PHP block continues until the physical end of the file, causing it to crash when it finally hits that final line without finding the expected delimiter.
Looking closely at the provided script, there is a distinct structural issue involving how PHP logic is interleaved with HTML and form elements. The script begins a PHP block early on:
<?php
// ... logic here ...
} else {
?> // This closes the PHP tag prematurely
</div> // HTML content follows immediately
<form action='' method='post'>
// ... form fields ...
</form>
<? } ?> // Another premature closing attempt mixed with opening logic
The core issue lies in the nested if/else block structure. The code executes a PHP block that checks isset($_POST['text']). Inside the else branch of this check, there is a ?> tag intended to close the block. However, immediately following that closure is HTML (the form), and then later in the file, there is an attempt to close something again with <? } ?>.
This creates two fatal problems:
- Premature Tag Closure: By placing
?>inside the middle of the file (specifically before the form definition), you terminate the PHP execution context. The subsequent HTML and any code after that line are treated as pure output until the next<?phptag appears. If the logic expects variables defined before theelseblock to be used after the form, or if the script intends for the file to continue running, closing the tag here breaks the flow. - Malformed Closing: The fragment
<? } ?>is syntactically invalid. You cannot open a PHP tag with<and close it with>while trying to execute a closing brace}in between without proper syntax. It looks like an attempt to wrap the logic but failed to adhere to standard PHP block delimiters.
The Fix: Consolidate Logic Blocks
To resolve this, you must ensure that all executable PHP code resides within a single, continuous block or properly separated blocks if conditional rendering is strictly required.
If the intention is to execute the search logic only when data is posted, and display a form otherwise, you should avoid mixing raw HTML between active PHP execution states unless using echo or output buffering correctly. However, in this specific script, it appears the developer intended to handle the "no POST" state by simply closing the tag and letting the form render.
Here is the corrected structure pattern:
<?php
$fname = strlen(htmlspecialchars($_POST['text']));
if (isset($_POST['text'])) {
$directory = "";
// Note: glob($directory . "*.txt") will return null if directory is empty string or invalid path
// Ensure the directory variable contains a valid path before calling glob.
$images = glob($directory . "*.txt");
foreach($images as $image) {
if($fname < 3) {
echo "You must have at least 4 characters in your search, Sorry.";
exit; // Use exit to stop execution immediately after an error message
}
$file = $image;
$searchfor = $_POST['text'];
if (file_exists($file)) {
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*$/mi";
if(preg_match_all($pattern, $contents, $matches)){
echo "<br><br>Found matches in $file:<br>";
$gud = implode("\n", $matches[0]);
echo nl2br(htmlspecialchars($gud));
}
}
}
} else {
// Do NOT close the PHP tag here with ?> if you want to keep executing logic or if
// the file structure relies on the block continuing.
// If the file ends after this else block, you must close it exactly once.
}
?>
Critical Implementation Details
Beyond the syntax error, there are functional flaws in the original code that will prevent the search from working even if the syntax is fixed:
- Empty Directory Path: The variable
$directoryis initialized as an empty string (""). Consequently,glob($directory . "*.txt")becomesglob("*.txt"). In a Windows environment like XAMPP, this searches the current directory relative to the script's location. If your.txtfiles are not immediately adjacent tolookup.php, or if you intended to search a specific database folder, the glob function will return an empty array, resulting in no matches being found regardless of the input. You must define$directorywith the absolute or correct relative path (e.g.,"./data/"). - Logic Error in Length Check: The line
$fname = strlen(htmlspecialchars($_POST['text']));calculates the length immediately. If the POST variable is not set,htmlspecialcharsmight behave unexpectedly depending on the PHP version and configuration, though usually it handles nulls gracefully. However, if you enter a short string (less than 4 characters), the script echoes an error but continues into the loop due to a lack ofexitorbreak. The original code returns inside the loop for valid files, which is correct, but the initial check should arguably happen before entering the file iteration loop for efficiency.
Modern Context: Laravel vs. Legacy PHP
If you are maintaining legacy systems found on public source sites (which this script appears to be), you will often encounter these "spaghetti code" structures where PHP logic is tightly coupled with HTML layout. While functional, this approach is difficult to debug and maintain.
For modern development, frameworks like Laravel handle this separation of concerns automatically. In a Laravel application, you would move the search logic into a controller method, return a view only for the form, and pass data back or forward using variables rather than echoing HTML directly inside if/else blocks within a single file. However, if your goal is strictly to fix this XAMPP script without introducing a new framework, focus on balancing the PHP tags (<?php ... ?>) and ensuring every opening brace { has a corresponding closing brace } before the final tag closure.
Finally, ensure that sanitize.php is actually included where commented in the original code: require "sanitize.php";. If this file is missing, you will receive a fatal error regarding the missing include once syntax issues are resolved.