doutuobao4004 2018-05-25 17:05
浏览 303
已采纳

从网站在主机服务器上创建文本文件

I am trying to attempt to give users the ability to write a text file to a directory on the website's host server. A usage case would look like:

  1. User visits www.example.com/createtext
  2. User types in a text bot what they would want to be saved, "Hello" for example
  3. The server that the website is hosted on creates a text file in one of it's directories.
  4. On the server, there is now a text file in C:/somedirectory/dailytexts/ with "Hello" written inside of it

I have currently written a simple page with a form that will take in text information and save it to a text file with the name of the current date.

<script language="Javascript">
    var time = new Date();
    var formattedTime = time.toLocaleTimeString();

    function download(filename, text) {
      var pom = document.createElement('a');
      pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + 

    encodeURIComponent(text));
      pom.setAttribute('download', formattedTime);

      pom.style.display = 'none';
      document.body.appendChild(pom);

      pom.click();
      document.body.removeChild(pom);
    }
</script>

<form id="form" class="topBefore" onsubmit="download(this['name'].value, this['text'].value)">
    <textarea id="message" type="text" placeholder="Hello" name="text"></textarea>
    <input id="submit" type="submit" value="Download Text File">
</form>

This simply downloads the text file to the user's computer. Instead, I would like the file to be created on the host machine.

Is there anyway to do this? In particular, with just JavaScript or Java. But if it has to be done with something like PHP then that would work too.

  • 写回答

1条回答 默认 最新

  • dongpo7467 2018-05-25 18:33
    关注

    AFAIK, JS in the browser only lets you do stuff client-side and not modify server content without the use of... (for example) PHP scripts on the server-side.

    This solution (now) uses only HTML and PHP, with the need of JS eliminated.

    HTML (modified):

    <form id="form" class="topBefore" action="createtext.php" method="GET">
    <textarea id="message" type="text" placeholder="Hello" name="name"></textarea>
    <textarea id="message2" type="text" placeholder="Hello" name="text"></textarea>
    <input id="submit" type="submit" value="Download Text File">
    

    PHP: (file named createtxt.php)

    if(isset($_GET['name']) && isset($_GET['text']))  {
        $filename = preg_replace('#[^A-Za-z0-9_-]#', '', $_GET['name']);
        $file = $_SERVER['DOCUMENT_ROOT']."/textfiles/$filename.txt";
        $f = fopen($file, 'w');
        fwrite($f, $_GET['text']);
        fclose($f);
        echo 'Success.';
    }  else  {
        echo 'Error.';
    }
    

    The file creation requires two parameters - the filename and content, while only one textarea was provided in the question's HTML. So another is included now, for the filename. jQuery/JS is removed. Instead, the form now sends the data over to createtext.php, which creates the file (fopen), writes the text (fwrite) and echoes "Success." - note two things here:

    1. We're saving the text-file to a folder called textfiles in the same directory as createtext.php. The folder must exist from before - it will not be created automatically, and PHP will throw an error if it doesn't.
    2. You can replace echo 'Success.'; with other actions: like redirecting to another page or the same page (header), or echoing/readfile-ing an HTML page.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符