如何将exec输出回显至HTML表格?附现有CSS与PHP代码
Alright, let's get your exec output displayed in an HTML table while leveraging your existing status circles and error message function. Here's a straightforward implementation:
Step 1: Capture Exec Command Output & Status
First, we'll use PHP's exec() function properly to capture both the command's output lines and its return status code (which your ReturnError() function relies on). The exec() function takes three key parameters:
- The command you want to run
- An array to store each line of the command's output
- A variable to hold the command's return status code
Step 2: Integrate with Your Existing Code & Build the Table
We'll combine your existing CSS, error function, and the exec data to render a table that shows the status circle, command output, and error message (if applicable).
Here's the full integrated code:
<!DOCTYPE html> <html> <head> <style> #circleRed { background: #ff0000; width: 15px; height: 15px; border-radius: 50%; display: inline-block; /* Added to align circles with text */ } #circleGreen { background: #00ff00; width: 15px; height: 15px; border-radius: 50%; display: inline-block; /* Added to align circles with text */ } table { border-collapse: collapse; margin-top: 20px; width: 80%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <?php function ReturnError($res) { switch ($res) { case "0": return "No errors"; case "-1": return "Video not found. This could be due to an invalid path or missing file."; default: return "Unknown error (code: $res)"; // Fallback for unexpected status codes } } // Replace this with your actual command to execute $command = "your-target-command-here"; $outputLines = []; $returnStatus = 0; // Run the command and capture output + status exec($command, $outputLines, $returnStatus); ?> <!-- Top-level Status Indicator --> <div> <?php if ($returnStatus === 0): ?> <div id="circleGreen"></div> <span>Command executed successfully</span> <?php else: ?> <div id="circleRed"></div> <span>Command failed</span> <?php endif; ?> </div> <!-- Exec Output Table --> <table> <thead> <tr> <th>Status</th> <th>Output Line</th> <th>Error Message</th> </tr> </thead> <tbody> <?php if (empty($outputLines)): ?> <tr> <td><?php echo $returnStatus === 0 ? '<div id="circleGreen"></div>' : '<div id="circleRed"></div>'; ?></td> <td>No output returned from the command</td> <td><?php echo ReturnError((string)$returnStatus); ?></td> </tr> <?php else: ?> <?php foreach ($outputLines as $line): ?> <tr> <td><?php echo $returnStatus === 0 ? '<div id="circleGreen"></div>' : '<div id="circleRed"></div>'; ?></td> <td><?php echo htmlspecialchars($line); ?></td> <td><?php echo ReturnError((string)$returnStatus); ?></td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </body> </html>
Key Details to Note:
- Circle alignment: Added
display: inline-blockto the circle CSS so they line up properly with text in the table and status header. - Security: Used
htmlspecialchars()to escape exec output, preventing potential XSS vulnerabilities if the command returns user-controlled content. - Fallback handling: Added a default case to
ReturnError()to handle unexpected status codes you might encounter. - Empty output support: The table will show a clear message if the command doesn't return any lines of output.
Just replace "your-target-command-here" with the actual command you want to run, and this will render a clean, functional table with your status indicators and error messages.
内容的提问来源于stack exchange,提问作者Kai

