【问题标题】:PHP write file from input to txtPHP将文件从输入写入txt
【发布时间】:2018-06-22 23:43:11
【问题描述】:

我在这个网站上搜索了答案,但找不到任何答案。

我有一个表单,我想将输入的内容写入一个 txt 文件。为了简单起见,我只写了一个简单的表单和一个脚本,但它总是让我看到一个空白页。这是我得到的

<html>
<head>
    <title></title>
</head>
<body>
    <form>
        <form action="myprocessingscript.php" method="post">
        <input name="field1" type="text" />
        <input name="field2" type="text" />
        <input type="submit" name="submit" value="Save Data">
    </form>
    <a href='data.txt'>Text file</a>
</body>

这是我的 PHP 文件

<?php
$txt = "data.txt"; 
$fh = fopen($txt, 'w+'); 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
   $txt=$_POST['field1'].' - '.$_POST['field2']; 
   file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt 
   exit();
}
    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file
    ?>

【问题讨论】:

  • myprocessingscript.php 是否产生任何进一步的输出?如果您的第二个 sn-p 是整个 PHP 脚本,那么您应该得到一个空白页面,因为您没有生成任何输出。
  • 你得到一个空白页也是正常的,你对客户没有任何echo()ing。

标签: php


【解决方案1】:

您的表单应如下所示:

<form action="myprocessingscript.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

和 PHP

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
    $ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

我写信给/tmp/mydata.txt,因为这样我就知道它在哪里了。使用 data.txt 写入当前工作目录中的该文件,在您的示例中我一无所知。

file_put_contents 为您打开、写入和关闭文件。不要乱来。

进一步阅读: file_put_contents

【讨论】:

  • 我把它改回 'data.txt' 但其余部分都很完美
  • 在您无法访问/tmp 的共享主机上,您会将mydata.txt 放在哪里?我的目录结构是 ~/www-root/
  • 非常感谢您在这里度过的时光,弗洛比。这是一个非常优雅的解决方案,让我可以放弃 3rd 方表单处理器所需的大量脚本,例如 WordPress 插件提供的脚本。 :)
【解决方案2】:

您遇到的问题是因为您有额外的&lt;form&gt;,您的数据进入GET 方法,而您正在使用POST 访问PHP 中的数据。

<body>
<!--<form>-->
    <form action="myprocessingscript.php" method="POST">

【讨论】:

    【解决方案3】:

    一个可能的解决方案:

    <?php
    $txt = "data.txt"; 
    if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
        $fh = fopen($txt, 'a'); 
        $txt=$_POST['field1'].' - '.$_POST['field2']; 
        fwrite($fh,$txt); // Write information to the file
        fclose($fh); // Close the file
    }
    ?>
    

    您在关闭文件之前关闭了脚本。

    【讨论】:

      【解决方案4】:

      如果您使用 file_put_contents,您不需要执行 fopen -> fwrite -> fclose,file_put_contents 会为您完成所有这些。您还应该检查网络服务器是否在您尝试写入“data.txt”文件的目录中具有写入权限。

      根据您的 PHP 版本(如果它是旧的),您可能没有 file_get/put_contents 函数。检查您的网络服务器日志以查看执行脚本时是否出现任何错误。

      【讨论】:

      • 我注意到如果我在服务器上使用管理员运行它可以正常工作,我该如何更改权限?
      • 如果你的网络服务器是 apache 例如:chown -R apache:apache dir_where_you_create_the_file
      【解决方案5】:

      使用fwrite() 代替 file_put_contents()

      【讨论】:

        猜你喜欢
        • 2013-10-03
        • 1970-01-01
        • 2012-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-03
        相关资源
        最近更新 更多