从网站上的用户输入写入文本文件以填充变量

时间:2015-09-01 14:14:07

标签: php file shell

我正在尝试使用用户输入对话框创建一个简单的网站,以填充将在 shell命令中使用的变量。

我知道最简单的事情可能是使用HTML表单,然后调用php文件写入文件。我找到了一些类似的代码但是如何使用输入来填充变量呢?示例如下。

我希望最终结果如下所示。

  

testapp -d -p -e MySQL_dbname = $ variable1 -e MySQL_dbuser = $ variable2 -e MySQL_server = $ variable3 -e MySQL_pass = $ variable3

这是我已经找到的代码

<form action="myprocessingscript.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

<?php
    if(isset($_POST['field1']) && isset($_POST['field2'])) {
        $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
        $ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
        if($ret === false) {
     die('There was an error writing this file');
}
else {
    echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}

1 个答案:

答案 0 :(得分:0)

虽然每个变量都打印到一个新行,但我能够通过使用连接获得我之后的输出。有什么想法我怎么能把它输出到同一条线上?

<?php
if(isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3']) && isset($_POST['field4']) && isset($_POST['field5'])) {
    $containername = $_POST['field1'] . "\n";
    $WPhost = $_POST['field2'] . "\n";
    $WPuser = $_POST['field3'] . "\n";
    $WPpass = $_POST['field4'] . "\n";
    $WPname = $_POST['field5'] . "\n";
    $WPapp = 'testapp run -d --name';
    $WPdbhost = '-e WORDPRESS_DB_HOST=';
    $WPdbuser = '-e WORDPRESS_DB_USER=';
    $WPdbpass = 'WORDPRESS_DB_PASSWORD=';
    $WPdbname = '-e WORDPRESS_DB_NAME=';
    $ret = file_put_contents('/tmp/mydata.txt', $WPapp . ' ' . $WPhost . $WPdbhost . $WPuser . $WPdbuser . $WPpass . $WPdbname . $WPname, FILE_APPEND | LOCK_EX);
    if($ret === false) {
 die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}