php - 使用 PHP 将文本区域的内容保存到 .txt 文件

标签 php

我有一个代码,应该使用 PHP 将文本区域中的内容保存为文本文件,如果我单击按钮,就会创建文件,但不会在其中写入任何数据。

代码:

           <form  method="POST" action="">
              <div data-label="Index.php" class="demo-code-preview" contentEditable="true">
                          <pre><code class="language-html">
                              <textarea name="content">
                            <?php
                              $file = "index.php";
                              echo(htmlspecialchars(file_get_contents($file)));
                              ?>
                            </textarea>
                          </code></pre>
              </div>
            </a>
              <a href="?SubmitFile=true" class="bar-anchor" name="SaveFile">
               <span>Save</span>
            <div class="transition-bar"></div>
            </a>
          </form>



<?php
if (isset($_GET['SubmitFile'])){
    $content = $_POST['content'];
    echo "<script>console.log('" . $content . "' );</script>";
    $file = "myfile.txt"; // cannot be an online resource
    $Saved_File = fopen($file, 'a+');
    fwrite($Saved_File, $content);
    fclose($Saved_File);
  }
?>

最佳答案

您实际上并未提交表单,只是在单击链接时将 SubmitFile 参数作为 GET 参数传递。最简单的方法是将链接替换为提交按钮,然后更改 PHP 以在 POST 中查找该参数。

如果您确实需要使用链接来提交表单,则需要使用一些 JavaScript 来取消链接上的默认事件并提交表单。在这种情况下,您需要将 SubmitFile 参数添加为隐藏字段。

<form method="POST" action="">
    <div data-label="Index.php" class="demo-code-preview" contentEditable="true">
          <pre><code class="language-html">
              <textarea name="content">
            <?php
            $file = "index.php";
            echo(htmlspecialchars(file_get_contents($file)));
            ?>
            </textarea>
          </code></pre>
    </div>
    <!-- 
    This does not submit the form, it only makes a get request with the SubmitFile parameter.
    You need ot submit the form and send the SubmitFile in the post, or add the SubmitFile
    parameter to the form action if you really want to see it in the GET params.
       
    <a href="?SubmitFile=true" class="bar-anchor" name="SaveFile"><span>Save</span></a>
    -->
    <button type="submit" name="SubmitFile">Save</button>
</form>


<?php
/*
 * This will be true if the parameter is in the GET, but does not guarantee that
 * the form was posted, so you cannot rely on this as a GET parameter.
 * Change it to POST in the markup and here.
 * 
 * if (isset($_GET['SubmitFile']))
 */
if (isset($_POST['SubmitFile']))
{
    $content = $_POST['content'];
    echo "<script>console.log('" . $content . "' );</script>";
    $file       = "myfile.txt"; // cannot be an online resource
    $Saved_File = fopen($file, 'a+');
    fwrite($Saved_File, $content);
    fclose($Saved_File);
}
?>

关于php - 使用 PHP 将文本区域的内容保存到 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68362004/

相关文章:

php - 我如何让这个小 PHP 数组工作?

PHP min 和 max 带数组,靠谱吗?

PHP数据库获取变量

PHP+Imagick - PNG压缩

php - 如何查询和排列两个相似的表

php - Joomla K2 模块中的表注释自动插入记录

php - 避免删除字符串前缀中不需要的字符

php - 使用 cURL 上传多个文件

php - 如何设置 php 脚本来处理 2 个表

php - 如何在 PHP 中通过 URL 传递多个变量?