【问题标题】:PHP: Form to txt filePHP:表格到txt文件
【发布时间】:2015-09-02 11:21:08
【问题描述】:

我一直在开发一个测试网站,该网站使用 PHP 脚本将数据转储到 txt 文件中,以实现无数据库选项。

我的XAMPP 设置说我的 post 变量未定义,但我确定不是。

我尝试使用正确的名称(空白)预先创建文本文件,并且我没有弄乱我的 Mac 上的权限。

如何编写这个脚本?

【问题讨论】:

  • 请分享您尝试过的代码
  • 请发布您的代码 - 后端和发送表单的 HTML...
  • 嘿,莱伊。您是否尝试过简单的var_dump($_POST,$_GET); 来查看表单是否正确提交?它可能是空的,因为它是作为 GET 请求发送的...

标签: php html xampp


【解决方案1】:

在你的 php 文件中运行它,你应该会在你的 php 文件所在的文件夹中看到 myfile.txt。

$filepath = dirname(__FILE__) . '/myfile.txt'; //Current folder and this filename
$text = 'New Line of text'; //You input text

if(file_exists($filepath)) //If file created, append text
{
   file_put_contents($filepath, $text, FILE_APPEND);
}else{
   file_put_contents($filepath, $text);
}

使用var_dump($_POST); 查看您的帖子。确保表单设置为method='POST'

【讨论】:

    【解决方案2】:

    确保您已将 PHP 保存到文本文件的权限以及文件本身设置为可写,这样应该可以解决您的问题。

    <form method="post" action="?submitted=true">
        <input type="text" name="itemA"/>
        <input type="text" name="itemB"/>
        <input type="text" name="itemC"/>
        <button type="submit"><button>
    </form>
    <?php
        if(isset($_GET['submitted']) && isset($_POST)) {
            $file = "textfile.txt"; 
            $text = "";
            foreach($_POST as $item) {
                $text .= $item;
            }
    
            if($text > "") {
                file_put_contents($file, $text);
            } else {
                echo "No items in POST!";
            }
        }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-28
      • 2018-08-18
      • 2017-01-26
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      相关资源
      最近更新 更多