简体   繁体   English

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

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

NEW SITUATION/QUESTION新情况/问题

This is the same question, but I am trying it a different way.这是同一个问题,但我正在尝试不同的方式。 If you see what I did below and my comments with Rob, I was trying to do it with PHP, but I've spent the past few days trying to get PHP on my nginx server through Docker, and I've been having a really tough time.如果你看到我在下面做了什么以及我对 Rob 的评论,我试图用 PHP 来做,但过去几天我一直在尝试通过 Docker 在我的 nginx 服务器上获取 PHP,而且我真的很开心艰难的时间。

So, I decided to go back to the initial problem (the question in the title).所以,我决定回到最初的问题(标题中的问题)。 So, I tried to implement some other code online and something happens, but I just have a few questions about it.因此,我尝试在网上实现一些其他代码并且发生了一些事情,但我只是有几个问题。 With this code:使用此代码:

    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>

After typing in '1234' in the textarea and upon submit, it refreshed my page with the same url, but 'text=1234' was on the end.在 textarea 中输入“1234”并提交后,它使用相同的 url 刷新了我的页面,但“text=1234”在最后。 I'm just not sure where this txt document that I wanted to create is.我只是不确定我想创建的这个 txt 文档在哪里。 It's not in the filepath that I specified in the WriteToFile function.它不在我在 WriteToFile 函数中指定的文件路径中。 I'm not sure if this is because I'm doing this on Docker, a virtual host server?我不确定这是否是因为我在 Docker(一个虚拟主机服务器)上执行此操作? Any help regarding this would be great!任何有关这方面的帮助都会很棒!

OLD SITUATION/QUESTION旧情况/问题

I have this server from an IP address.我有一个来自 IP 地址的服务器。 I have other buttons and such that will take me to other pages.我有其他按钮,这样可以带我到其他页面。 I am trying to implement a textarea that will allow the user to paste text and then on a button click or 'submit' click will copy that text to a tmp txt file which I will run some pre-made scripts on.我正在尝试实现一个允许用户粘贴文本的文本区域,然后单击按钮或“提交”单击将将该文本复制到 tmp txt 文件中,我将在该文件上运行一些预制脚本。 I'm having a really hard time figuring out not only how to pass this text to a new txt file, but also accessing it through the server (through PUTTY via the IP address).我真的很难弄清楚不仅如何将此文本传递给新的 txt 文件,还要通过服务器(通过 IP 地址通过 PUTTY)访问它。 I believe I need to use PHP to do this, which I haven't used until today.我相信我需要使用 PHP 来做到这一点,直到今天我才使用它。 Any help would be greatly appreciated.任何帮助将不胜感激。 Here's what I have so far.这是我到目前为止所拥有的。 This is all in the same index.html file.这都在同一个 index.html 文件中。 I tried creating a filename.php file and letting the action=filename.php, but was getting similar results and don't really understand the difference.我尝试创建一个 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>

Upon clicking the submit button with this code, it brings me to a new page with error code 404 Page not Found.单击带有此代码的提交按钮后,它会将我带到一个错误代码为 404 Page not Found 的新页面。 If I change the method to GET instead of POST, then it will download a txt file using the Notepad application (which is closer to what I want), but I cannot find this new file under the file tree on the server.如果我将方法更改为 GET 而不是 POST,那么它将使用记事本应用程序下载一个 txt 文件(它更接近我想要的),但是我在服务器的文件树下找不到这个新文件。

The file extension should probly be .php, not .html, unless your server is configured to parse HTML as PHP, which I would recommend against.文件扩展名应该是 .php,而不是 .html,除非您的服务器配置为将 HTML 解析为 PHP,我建议您不要这样做。 The action of the form can be empty, since you are submitting to the same file, so action="" as @LawrenceCherone mentioned in his comment.表单的操作可以为空,因为您要提交到同一个文件,所以 action="" 正如@LawrenceCherone 在他的评论中提到的那样。

Unless you really need the text file to be publicly available, best practice is to store it somewhere outside the document root.除非您确实需要公开文本文件,否则最佳做法是将其存储在文档根目录之外的某个位置。 Since you mentioned accessing it via PUTTY, it sounds like that won't be a problem.既然你提到通过 PUTTY 访问它,听起来这不会成为问题。 Your code works, but I recommend adding some error checking at a minimum.您的代码有效,但我建议至少添加一些错误检查。 The HTML also has some quirks I recommend ironing out. HTML 也有一些我建议解决的怪癖。 Here's a solid starting point.这是一个坚实的起点。

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