0

我刚开始从互联网学习 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);
    }

?>

请指导我..谢谢

4

1 回答 1

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);
      }
    ?>
于 2012-08-14T09:47:51.603 回答