简体   繁体   English

PHP 实时显示 Shell 命令 Output 在一个盒子里

[英]PHP Display Shell Command Output in a box in Real Time

I have the following code which asks for input then applies shell.sh $folderPath $bits $group2我有以下代码要求输入然后应用shell.sh $folderPath $bits $group2

And I would like to have a box where it displays the output of my script exactly how it would've looked if I ran in in a terminal.我想要一个盒子,它显示我的脚本的 output 如果我在终端中运行它会是什么样子。

How do I do that?我怎么做?

Below is my code.下面是我的代码。

<?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>

First of all, you need to send your form data somewhere.首先,您需要将表单数据发送到某个地方。 In your case, it is your same script.在您的情况下,这是您的相同脚本。

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

Secondly, shell_exec() returns output of executed command.其次, shell_exec()返回执行命令的 output。 You already wrote the code.您已经编写了代码。

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

Thirdly, display the output where you want.第三,在你想要的地方显示output。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM