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

如何将exec输出回显至HTML表格?附现有CSS与PHP代码

Solution: Echo Exec Output to HTML Table with Status Indicators

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-block to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:23:24