繁体   English   中英

如何将 textarea 的文本放入 html 所在服务器上的 .txt 文件中?

[英]How to put the text of a textarea into a .txt file on the server that the html is on?

新情况/问题

这是同一个问题,但我正在尝试不同的方式。 如果你看到我在下面做了什么以及我对 Rob 的评论,我试图用 PHP 来做,但过去几天我一直在尝试通过 Docker 在我的 nginx 服务器上获取 PHP,而且我真的很开心艰难的时间。

所以,我决定回到最初的问题(标题中的问题)。 因此,我尝试在网上实现一些其他代码并且发生了一些事情,但我只是有几个问题。 使用此代码:

    function textbox(){
        document.getElementById("textInput").className="show";
}

function WriteToFile(passForm) {
     var fso = CreateObject("Scripting.FileSystemObject");
     var s   = fso.CreateTextFile("/home/bsluss/b_test/CTO-Signing-Server/signing-server/www/cgi-bin/tmp.txt", True);

     var textinfo = document.getElementById('message');

     s.writeline(textinfo);
     s.close();
}

</script>

...

<input type="button" value="Add Cloud Info" onclick="textbox()" />
<div id="textInput" class="hide">
        <form id="form" class="topBefore" onSubmit="WriteToFile(this)">
  <textarea type="text" name="text" id="message" value=""></textarea><br>
  <input id="submit"  type ="submit" value="Copy to File"></input>
        </form>
</div>

</body>
</html>

在 textarea 中输入“1234”并提交后,它使用相同的 url 刷新了我的页面,但“text=1234”在最后。 我只是不确定我想创建的这个 txt 文档在哪里。 它不在我在 WriteToFile 函数中指定的文件路径中。 我不确定这是否是因为我在 Docker(一个虚拟主机服务器)上执行此操作? 任何有关这方面的帮助都会很棒!

旧情况/问题

我有一个来自 IP 地址的服务器。 我有其他按钮,这样可以带我到其他页面。 我正在尝试实现一个允许用户粘贴文本的文本区域,然后单击按钮或“提交”单击将将该文本复制到 tmp txt 文件中,我将在该文件上运行一些预制脚本。 我真的很难弄清楚不仅如何将此文本传递给新的 txt 文件,还要通过服务器(通过 IP 地址通过 PUTTY)访问它。 我相信我需要使用 PHP 来做到这一点,直到今天我才使用它。 任何帮助将不胜感激。 这是我到目前为止所拥有的。 这都在同一个 index.html 文件中。 我尝试创建一个 filename.php 文件并让 action=filename.php,但得到了类似的结果,并没有真正理解其中的区别。

<?php

if(isset($_POST["text"])) {
  $txt = $_POST["text"];
  $file = $_SERVER['DOCUMENT_ROOT'].'/tmp/tmpdata.txt';
  $fp = fopen($file, "a+");
  fwrite($fp, $txt . PHP_EOL);
  fclose($fp);
  echo 'Success.';
} else{
  echo 'Error.';
}
?>

<html>
<body>

 <form id="form" class="topBefore" action="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>" method="POST">
  <textarea type="text" name="text" id="message" value=""></textarea><br>
  <input id="submit"  type ="submit" value="Copy to File"></input>
        </form>
</body>
</html>

单击带有此代码的提交按钮后,它会将我带到一个错误代码为 404 Page not Found 的新页面。 如果我将方法更改为 GET 而不是 POST,那么它将使用记事本应用程序下载一个 txt 文件(它更接近我想要的),但是我在服务器的文件树下找不到这个新文件。

文件扩展名应该是 .php,而不是 .html,除非您的服务器配置为将 HTML 解析为 PHP,我建议您不要这样做。 表单的操作可以为空,因为您要提交到同一个文件,所以 action="" 正如@LawrenceCherone 在他的评论中提到的那样。

除非您确实需要公开文本文件,否则最佳做法是将其存储在文档根目录之外的某个位置。 既然你提到通过 PUTTY 访问它,听起来这不会成为问题。 您的代码有效,但我建议至少添加一些错误检查。 HTML 也有一些我建议解决的怪癖。 这是一个坚实的起点。

<?php
// Set up a status flag end message string
$status = 'init';
$msg = '';

// If the submit button was pressed, attempt to process the post
if (isset($_POST["submit"]))
{
    try
    {
        // Make sure we have text, if not, return an error
        if (empty($_POST["text"]))
        {
            throw new Exception('Please enter text');
        }

        $txt = $_POST["text"];

        /*
         * Path to the file - it's a good practice to place this outside the
         * document root so it's not publicly available or executable by the
         * server.
         */
        $file = $_SERVER['DOCUMENT_ROOT'] . '/tmp/tmpdata.txt';

        // Attempt to open a file handle, If we can't, bail out with an error
        if (($fp = fopen($file, "a+")) === false)
        {
            throw new Exception('Unable to open file for writing');
        }

        if(fwrite($fp, $txt . PHP_EOL) === false)
        {
            throw new Exception('Unable to write to file');
        }

        fclose($fp);

        // Set the status to success
        $status = 'success';
    }
    catch(Exception $e)
    {
        $status = 'error';
        $msg    = $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<?php if ($status == 'error') { ?>
    <div class="alert alert-error"><?php echo $msg; ?></div>
<?php } elseif ($status == 'success') { ?>
    <div class="alert alert-success">Text stored successfully</div>
<?php } ?>
<body>
<form id="form" class="topBefore" action="" method="POST">
    <textarea name="text" id="message"></textarea><br>
    <input name="submit" type="submit" value="Copy to File">
</form>
</body>
</html>

暂无
暂无

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

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