获取用户输入到php文件

时间:2012-08-14 09:36:36

标签: php

我刚刚开始从互联网上学习php。我看过一些文件处理的例子。但是当我按照文件写入的相同程序时,它无法正常工作。

阅读功能是工作文件。但是写入文件不起作用。我也尝试使用 file_put_content 函数来编写。 :(

<?php
    if(isset($_POST['submit1'])){
    $fileName = "Text.txt";
    $fh = fopen($fileName,"a") or die("can not open the file to write");

    //Writing to a file
    $newData = "Hello This is new Data";
    fwrite($fh,$newData);
    fclose($fh);

    //Reading a file -- Working
    $text = fopen("Text.txt","r") or die ("can not open the file to read");     
    while(!feof($text))
    {
        $myLine = fgets($text);
        print $myLine;
    }
    fclose($text);
    }

?>

请指导我.. 感谢

1 个答案:

答案 0 :(得分:1)

这很好,你得到的错误是什么?

  <?php
    $file = 'text.txt';
    $writer = fopen($file, 'a');
    $addData = 'This is a new string to be added at the end of the file';
    fwrite($writer, $addData);
    fclose($writer);
  ?>

EDIT1: 要从POST请求输入输入,您可以执行以下操作:

    <?php
      if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $addData = $_POST['input-name'];
        $file = 'text.txt';
        $writer = fopen($file, 'a');

        fwrite($writer, $addData);
        fclose($writer);
      }
    ?>