首页 > 解决方案 > PHP在一个框中实时显示Shell命令输出

问题描述

我有以下代码要求输入然后适用shell.sh $folderPath $bits $group2

而且我想有一个盒子,它可以准确地显示我的脚本的输出,如果我在终端中运行它会是什么样子。

我怎么做?

下面是我的代码。

<?php
    if (isset($_GET['folderPath']) && !empty($_GET['folderPath']) && file_exists($_GET['folderPath']) 
    && is_dir($_GET['folderPath']) && isset($_GET['bits']) && isset($_GET['group2'])) {

        $folderPath = $_GET['folderPath'];
        $bits = $_GET['bits'];
        $group2 = $_GET['group2'];

        $run = shell_exec("shell.sh $folderPath $bits $group2");
        echo "shell.sh $folderPath $bits $group2";
        var_dump($run);
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>
<body>
    <form action="">
        <label for="folderPath">Folder Path:</label>
        <input type="text" id="folderPath" name="folderPath">
    <br></br>
        <fieldset id="bits">
            <p>Please Choose bits: </p>
          <input type="radio" value="8bits" name="bits"> 8bits <br>
          <input type="radio" value="16bits" name="bits"> 16bits <br>
        </fieldset>
    <br></br>
        <fieldset id="audio-type">
            <p>Please Choose Type: </p>
          <input type="radio" value="C12" name="group2"> C12 <br>
          <input type="radio" value="LR" name="group2"> LR <br>
        </fieldset>
        <input class = "submitButton" type="submit" value="Submit">
</body>
</html>

标签: javascriptphphtml

解决方案


首先,您需要将表单数据发送到某个地方。在您的情况下,这是您的相同脚本。

<form method="GET" action="path/to/my/script.php">

其次,shell_exec()返回执行命令的输出。您已经编写了代码。

$path = $_GET['folderPath'];
$bits = $_GET['bits'];
$group2 = $_GET['group2'];
$output = shell_exec("shell.sh $path $bits $group2");

第三,在您想要的位置显示输出。

<div><?php print $output; ?></div>

推荐阅读